![]() Where the helper method RoundCurrentToNextFiveMinutes() looks like this: DateTime RoundCurrentToNextFiveMinutes() StartTime, TimeSpan.FromMinutes(5), tokenSource.Token) Task timerTask = RunPeriodically(sendRequest, TimeSpan delay = _nextRunTime - DateTime.UtcNow Ĭalled like this: CancellationTokenSource tokenSource = new CancellationTokenSource() ĭateTime startTime = RoundCurrentToNextFiveMinutes() For example: // private field somewhere appropriate it would probably be best to putĪsync Task RunPeriodically(Action action,ĭateTime startTime, TimeSpan interval, CancellationToken token) If instead you do want to perform the action exactly on five minute intervals, you can do something similar, but compute the next run time and delay for an appropriate amount of time. Otherwise, the sendRequest() method will be called every five minutes (with it being called immediately when the RunPeriodically() method is called…you can reorder the statements in the loop if you want it to wait the first time too). When tokenSource.Cancel() is called, the loop will be interrupted by the TaskCanceledException thrown at the await Task.Delay(.) statement. Task timerTask = RunPeriodically(sendRequest, TimeSpan.FromMinutes(5), tokenSource.Token) ![]() It can be called like this: CancellationTokenSource tokenSource = new CancellationTokenSource() Then that can be implemented very easily, like this: async Task RunPeriodically(Action action, TimeSpan interval, CancellationToken token) ![]() it's not that important that it run exactly on five-minute divisions of the hour). Then, let's assume that the basic requirement is to run it roughly every five minutes (i.e. Also, I differ a little on the specifics of the implementation, such as how to manage the delay computation and how to round the current time to the next five minute interval.įirst, let's assume the sendRequest() method returns void and has no parameters. However, IMHO with modern C# it is now better to use the Task-based API with async and await. The answer posted six years ago is useful. You can probably get away with a value smaller than one second but I'll leave that to you. Note that the tolerance is necessary in case this code is executing when now is very close to the nearest hh:mm with mm % 5 = 0. Var Timer = new Timer(callback, null, timeToStart, TimeSpan.FromMinutes(5)) TimeSpan tolerance = TimeSpan.FromSeconds(1) TimeSpan timeToStart = nearestOnFiveMinutes.Subtract(now) Int additionalMinutes = 5 - now.Minute % 5 Note that you'll need to keep your application alive somehow (e.g., run it as a service).Īs for how to make sure that it runs at hh:mm where mm % 5 = 0, you can do the following. You can use the second parameter to pass state to the callback. You can specify a method to call periodically.Įxample: Timer timer = new Timer(Callback, null, TimeSpan.Zero, TimeSpan.FromMinutes(5)) Ĭonsole.WriteLine("The current time is ", DateTime.Now)
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |