Periodic Timer in C# Including .NET 8 Updates

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
the periodic timer allows us to run tasks on a timer in a way that is efficient and asynchronous instead of using a Threading timer a UI timer or a dispatcher timer and depend on callbacks we can use this one timer to safely execute our code let's see how they work in this 10-minute training video now for most of my training I work to give an indepth perspective on a technology but sometimes you just need a quick answer to the question how do I use this that's why I created this 10-minute training Series so here I have some example code and I put it in a console application typically we see these periodic timers use most in web applications or even desktop applications where you want to run have something running in the background but I want to put in a console application so you can see exactly what's going on without having to have all the extra stuff around it so this is all there is to a periodic timer the first thing is you need declare a periodic timer now I've used the using statement here so that when this application closes it will properly dispose of this timer now when you declare and instantiate it you have to give it a time span in this case I've said a time span of 1,000 milliseconds or 1 second and you can give it whatever time span you want from one to all the way up to to uh except for the max value one below the max value for a time span now you can give an infinite time span we'll talk about that in a minute so what's gonna happen is this timer is going to tick or run across that 1 second threshold every second and we can change this to five and that' be every five seconds it would fire this timer now what I've done here is I created a while loop and I said while a wait timer. wait for next tick async what that means is it's going to wait for that timer to to complete and it's going to then do something in this code in this case we're just writing out to the console and we're incrementing I by one and it will continue to Loop that way forever except for the fact that I put this and I is less than five so we'll only execute it five times before we exit the while loop now that does not stop the timer from running what it does that we've stopped listening for it so the time will run again is just that we're not listening for it so nothing happens and when we're done it's going to say we are done and then we can you know hit the enter key and we'll close out of our application at that point we'll dispose the timer so let's just see us running first to see in action and then we're going to kind of see some of the different parts of this so here we have our timer running and as you can see see it's every tick or every second it's running that event execute number one ran at 51435 and this last here is the thousandths of a second and notice they're really close but they're not exactly the same this is to be clear this is a timer that runs every every period as you ask but it's not going to be exactly precise because it does depend on the code that runs inside these curly braces where if this code runs for longer than the tick let's just put a uh a delay in here we're going to say task dot let's await task. delay we're I say a delay of uh let's do 3,000 for 3,000 milliseconds which going 3 seconds now what do you think will happen here because we're delaying inside this this call well first one ran right out after 1 second but then we have a 1 second or 3 second Gap and a 3 second Gap and a 3se second Gap and another 3 second Gap is g to come up so it still ran five times but we have this Gap in between so what happened because this timer supped to run every second well that's true but we're awaiting for it and then doing some code and then awaiting again so in the meantime even though the timer has hit multip or could have hit multiple times it just hits and it waits so it's waiting for us to come back around and say okay now the next tick and it's already waiting for us therefore it just says yes I I've been hit so that's something to note is that if you're expecting this to be from you know every second from the start of one to the start of the next that's not not necessarily true it's going to be from the start of one plus however long it takes your code to run to then the next one so that's just something to think through now this all works with net 6 and above okay so net 6 and above but we are going to look at something that is only in net 8 this is a new feature in do net 8 and that the idea that you can change the period so let's get rid of this await and we're going to say timer. period and we can set whatever time span we want so in this case we could do two let's do this let's do um I times 1,00 so we're already have incremented I we don't have to write zero but 1 time 1,00 is one second 2 * 2 thou th000 is 2 seconds and so on so you can see how we can change the period as we run this so the first one we have 1 second then two seconds the next one will be 3 seconds the next will be 4 seconds and so on so we can change how long these take to run by changing the period as we're going we can change that period and and see the new time execute so this can be a way to make your timer kind of variable depending on the circumstances maybe you are checking for for new results every 15 minutes but then when you don't get new results you expect some maybe you shorten that next call down to every five minutes until you get results then you go back to 15 minutes or whatever you want to do for your different timer periods you can as I mentioned also set your timer. period to be equal to timeout. infinite timespan and what this would do is essentially turn your timer off it's saying hey you know what don't ever fire again and that might be something you want to do if you are you know running something somewhere else you you want to wait to re-execute the timer until after you've you've done some tasks maybe set it timer time out. infinite time span and then do some work and then set it back to whatever value you could do that if you want or have a pause feature for your timer or whatever so also possible just note that if that were the case then it's going to wait forever for your next tick and it won't happen so you wouldn't want to leave this this way unless you were going to change it somewhere else to we go back to a time span okay so that's the the periodic timer and by the way this is again these changes right here are net 8 changes okay the timer dot period be able to change that after the fact that's a net 8 thing all right also you can only have one thing look at this wait for next a tick a sync so you can't have multiple of them per instance waiting for that next tick a sync you would do all of your calls for different things inside CR races if you're doing it in a while loop you don't have to do it in while loop you can just say if timer. wa for next tick async dot is completed or is complete um you can check that and see if it's true so there's different ways of checking for it but typically this would be used for things like a web application where maybe you want to have the the cash refreshed every minute or two um but you don't want to have to trigger it based off a user's action so therefore you run this in the background and have it refresh that cach every minute or whatever and that way your cash is always refreshed but um you don't have to you know wait for a specific user event to to update that cache so things like that you could do um you could do other background tasks in a web application like you know sending out an email when new orders come in or whatever that case may be the same of desktop applications you can do background tasks there are more comprehensive systems for background tasks like hangfire and courtz and others but this is for simple things it's a really great timer to use across all different types of applications okay so that's it it runs asynchronously which means that we can do other things while we're waiting for this and so on great little tool to use I wouldn't use it necessarily in a console application but um you can use it in anynet application you want from net 6 and above all right thanks for watching and as always I am Tim [Music] Corey [Applause] [Music]
Info
Channel: IAmTimCorey
Views: 10,418
Rating: undefined out of 5
Keywords: .net, C#, Visual Studio, code, programming, tutorial, training, how to, tim corey, C# course, C# training, C# tutorial, .net core, vs2022, .net 6
Id: N7vhJ7O56iM
Channel Id: undefined
Length: 10min 11sec (611 seconds)
Published: Mon Jan 01 2024
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.