Periodic Timer in C# Including .NET 8 Updates

  Рет қаралды 14,847

IAmTimCorey

IAmTimCorey

Күн бұрын

Пікірлер: 28
@rikudouensof
@rikudouensof 10 ай бұрын
Thanks for the video Tim. Happy new year
@IAmTimCorey
@IAmTimCorey 10 ай бұрын
You are welcome. Happy New Year to you as well.
@nandkishorsonwale
@nandkishorsonwale 10 ай бұрын
Thank for great video. Happy New Year. 💐
@IAmTimCorey
@IAmTimCorey 10 ай бұрын
You are welcome. Happy New Year!
@BoSteffensen
@BoSteffensen 9 ай бұрын
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.
@bradleyhodgins6354
@bradleyhodgins6354 5 ай бұрын
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?
@IAmTimCorey
@IAmTimCorey 5 ай бұрын
Simple rule of thumb: if something implements IDisposable, use a using statement. That way you are sure things will be properly closed down.
@TheNoar12
@TheNoar12 10 ай бұрын
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++?
@IAmTimCorey
@IAmTimCorey 10 ай бұрын
"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.
@TheNoar12
@TheNoar12 10 ай бұрын
@@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_Dine
@News_n_Dine 10 ай бұрын
Hello Mr Corey. Do you have videos on other background task functionalities?
@IAmTimCorey
@IAmTimCorey 10 ай бұрын
Not yet, but I will.
@1MTEK
@1MTEK 9 ай бұрын
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?
@MaximNisenbaum
@MaximNisenbaum 9 ай бұрын
It seems to be firing at exact interval, despite supposed delay...
@vijayarajan-bt5fk
@vijayarajan-bt5fk 4 ай бұрын
🎉🎉🎉✨✨✨⭐
@IAmTimCorey
@IAmTimCorey 4 ай бұрын
👍
@Draco2199
@Draco2199 10 ай бұрын
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.
@IAmTimCorey
@IAmTimCorey 10 ай бұрын
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.
@Draco2199
@Draco2199 10 ай бұрын
@@IAmTimCorey Running latest. VS version is 17.8.3. I must be missing something.
@Quebster
@Quebster 10 ай бұрын
@@Draco2199your project itself still needs to be targeting .net 8
@Draco2199
@Draco2199 10 ай бұрын
Fixed it. Package conflict.
@dimimmobiliare
@dimimmobiliare 10 ай бұрын
thanks for the video! If i want set the execute code at the specific Time? is possibile only by Task manager in windows?
@IAmTimCorey
@IAmTimCorey 10 ай бұрын
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.
@LuisAngelSeguraMartinez
@LuisAngelSeguraMartinez 8 ай бұрын
@@IAmTimCorey Mr Corey do you have any video with those options?
@usernamename2978
@usernamename2978 10 ай бұрын
The mountain brought forth a mouse.
@IAmTimCorey
@IAmTimCorey 10 ай бұрын
Are you expecting every change Microsoft makes to be huge?
Command Line Arguments in C# Applications in 10 Minutes or Less
8:47
How IDisposable and Using Statements Work Together in C#
10:01
IAmTimCorey
Рет қаралды 32 М.
Wait… Maxim, did you just eat 8 BURGERS?!🍔😳| Free Fire Official
00:13
Garena Free Fire Global
Рет қаралды 9 МЛН
Увеличили моцареллу для @Lorenzo.bagnati
00:48
Кушать Хочу
Рет қаралды 3,9 МЛН
Из какого города смотришь? 😃
00:34
МЯТНАЯ ФАНТА
Рет қаралды 1,7 МЛН
How To Create Generics in C#, Including New Features
38:51
IAmTimCorey
Рет қаралды 50 М.
Scheduling repeating tasks with .NET 6’s NEW Timer
12:47
Nick Chapsas
Рет қаралды 107 М.
C# Events События
14:36
codaza
Рет қаралды 53 М.
Primary Constructors in .NET 8
28:42
IAmTimCorey
Рет қаралды 23 М.
Factory Pattern in C# with Dependency Injection
1:07:33
IAmTimCorey
Рет қаралды 109 М.
Intro to the CsvHelper Library for C#
27:12
IAmTimCorey
Рет қаралды 28 М.
What is Span in C# and why you should be using it
15:15
Nick Chapsas
Рет қаралды 259 М.
Timer / Time Tracking with Blazor in .NET 6 🔥
22:37
Patrick God
Рет қаралды 10 М.
Wait… Maxim, did you just eat 8 BURGERS?!🍔😳| Free Fire Official
00:13
Garena Free Fire Global
Рет қаралды 9 МЛН