Thanks for your excellent videos, Tim, I have learned a lot from these. However, regarding the elapsed time from one iteration of a loop to the next, I disagree with you. You should not add the execution time of one iteration of the loop to the timer period. If the timer period is 1000ms, the timer fires every 1000ms regardless of the time spent in the iteration. However, if the time spent exceeds the timer period, you cannot “consume” the timer signals, and the signal will then just remain set when the timer fires. That is, your loop code will run every 1000ms if the time spent is less than the timer period. @1MTEK ask how to get exactly 1000ms between each iteration. The answer is: it is not possible when running code on a multi-threaded operating system. You will never know when the OS decides to run your code. However, if you run several loop iterations, you will see that the time between one iteration and the next iteration sometimes is a little bit more than 1000ms, and sometimes is a little bit less than 1000ms, but on average it is 1000ms.
@bradleyhodgins63545 ай бұрын
Hello, and thank you for your videos. Regarding the 'using' statement. Why do you need it in this case? Why wouldn't the PeriodicTimer be disposed of correctly (I know this is down to my lack of understanding)? Is there a way to identify when a 'using' statement is required/a good idea?
@IAmTimCorey5 ай бұрын
Simple rule of thumb: if something implements IDisposable, use a using statement. That way you are sure things will be properly closed down.
@TheNoar1210 ай бұрын
Thank you for the video. Would it be a mean to have some more precise timing means in .net? Or is this something reserved for more low level languages like c or c++?
@IAmTimCorey10 ай бұрын
"Precision" when it comes to all things time is a tricky subject. It all depends on what you mean and what your requirements are. For instance, if you are measuring how long something takes in C#, using the Stopwatch class will be more precise. Some people aren't a fan of DateTime, but it is precise to the tick (one tenth of a microsecond). For most everyday operations, that's more than precise enough. When it comes to triggering events based upon time, that gets even trickier because you need to identify how best to call an event to check the time in a way that doesn't overload the CPU. If you check every five seconds, your event could fire up to five seconds late. There are systems out there that work on optimizing this so that they can check less often, but do so in a way that allows them to hit precise times. Hangfire and Quartz have systems like this, among others.
@TheNoar1210 ай бұрын
@@IAmTimCorey thanks for these info. I will take a look at quartz and Hangfire since I haven't heard of them until now. Basically the app I have in mind must complete a image acquisition, perform some treatment and send resulting value over the network every 10ms... We will flirt with limits but we are still on the POC step so we can study different possibilities
@News_n_Dine10 ай бұрын
Hello Mr Corey. Do you have videos on other background task functionalities?
@IAmTimCorey10 ай бұрын
Not yet, but I will.
@1MTEK9 ай бұрын
How would I modify this so that the timer fires exactly every 1,000ms, no matter how long the code in the while loop takes to execute?
@MaximNisenbaum9 ай бұрын
It seems to be firing at exact interval, despite supposed delay...
@vijayarajan-bt5fk4 ай бұрын
🎉🎉🎉✨✨✨⭐
@IAmTimCorey4 ай бұрын
👍
@Draco219910 ай бұрын
Hmm.. I can't seem to get timer.Period. It's not available for some reason. I have all the latest so it must be something else.
@IAmTimCorey10 ай бұрын
Are you in a .NET 8 project? That change was introduced with .NET 8 and C# 12. Also, check your Visual Studio version. It should be 17.8.2.
@Draco219910 ай бұрын
@@IAmTimCorey Running latest. VS version is 17.8.3. I must be missing something.
@Quebster10 ай бұрын
@@Draco2199your project itself still needs to be targeting .net 8
@Draco219910 ай бұрын
Fixed it. Package conflict.
@dimimmobiliare10 ай бұрын
thanks for the video! If i want set the execute code at the specific Time? is possibile only by Task manager in windows?
@IAmTimCorey10 ай бұрын
You could have your timer check the time and only execute if the time matched what you wanted. However, you are probably better off using Hangfire, Quartz, or another scheduling system for .NET apps.
@LuisAngelSeguraMartinez8 ай бұрын
@@IAmTimCorey Mr Corey do you have any video with those options?
@usernamename297810 ай бұрын
The mountain brought forth a mouse.
@IAmTimCorey10 ай бұрын
Are you expecting every change Microsoft makes to be huge?