Periodic Timer in C# Including .NET 8 Updates

Поделиться
HTML-код
  • Опубликовано: 10 янв 2025

Комментарии • 28

  • @rikudouensof
    @rikudouensof Год назад +2

    Thanks for the video Tim. Happy new year

    • @IAmTimCorey
      @IAmTimCorey  Год назад

      You are welcome. Happy New Year to you as well.

  • @nandkishorsonwale
    @nandkishorsonwale Год назад +2

    Thank for great video. Happy New Year. 💐

    • @IAmTimCorey
      @IAmTimCorey  Год назад

      You are welcome. Happy New Year!

  • @BoSteffensen
    @BoSteffensen 10 месяцев назад +1

    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.

  • @1MTEK
    @1MTEK 11 месяцев назад

    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?

  • @bradleyhodgins6354
    @bradleyhodgins6354 7 месяцев назад

    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  7 месяцев назад +1

      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 Год назад

    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  Год назад +1

      "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 Год назад

      @@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 Год назад

    Hello Mr Corey. Do you have videos on other background task functionalities?

  • @MaximNisenbaum
    @MaximNisenbaum 11 месяцев назад

    It seems to be firing at exact interval, despite supposed delay...

  • @Draco2199
    @Draco2199 Год назад

    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  Год назад

      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 Год назад

      @@IAmTimCorey Running latest. VS version is 17.8.3. I must be missing something.

    • @Quebster
      @Quebster Год назад

      @@Draco2199your project itself still needs to be targeting .net 8

    • @Draco2199
      @Draco2199 Год назад

      Fixed it. Package conflict.

  • @dimsolutions
    @dimsolutions Год назад

    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  Год назад +1

      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 10 месяцев назад

      @@IAmTimCorey Mr Corey do you have any video with those options?

  • @vijayarajan-bt5fk
    @vijayarajan-bt5fk 6 месяцев назад

    🎉🎉🎉✨✨✨⭐

  • @usernamename2978
    @usernamename2978 Год назад

    The mountain brought forth a mouse.

    • @IAmTimCorey
      @IAmTimCorey  Год назад +2

      Are you expecting every change Microsoft makes to be huge?