Questions tagged [periodictimer]

The PeriodicTimer is a .NET class that enables waiting asynchronously for timer ticks. It was introduced in .NET 6.

Unlike most other timer-like .NET components that have a minimum timespan of TimeSpan.Zero, the PeriodicTimer.WaitForNextTickAsync method has a minimum timespan of 1 millisecond (citation).

10 questions
143
votes
11 answers

Calling a method every x minutes

I want to call some method on every 5 minutes. How can I do this? public class Program { static void Main(string[] args) { Console.WriteLine("*** calling MyMethod *** "); Console.ReadLine(); } private MyMethod() …
user1765862
  • 13,635
  • 28
  • 115
  • 220
37
votes
3 answers

Run async method regularly with specified interval

I need to publish some data to the service from the C# web application. The data itself is collected when user uses the application (a kind of usage statistics). I don't want to send data to the service during each user's request, I would rather…
Eadel
  • 3,797
  • 6
  • 38
  • 43
7
votes
2 answers

.NET 6 PeriodicTimer with top-of-the-minute timing

.NET 6 introduced the PeriodicTimer. I need to do something every minute, at the top of the minute. For example: 09:23:00, 09:24:00, 09:25:00, ... But with a one minute period - new PeriodicTimer(TimeSpan.FromMinutes(1)) - and starting at 09:23:45,…
lonix
  • 14,255
  • 23
  • 85
  • 176
2
votes
1 answer

Does .NET 6's PeriodicTimer capture the current SynchronizationContext by default?

For PeriodicTimer (AsyncTimer at the time), regarding WaitForNextTickAsync, David Fowler mentioned "The execution context isn't captured" (here) via (here). However, given that was not necessarily the final implementation, I reviewed the…
Novox
  • 774
  • 2
  • 7
  • 24
1
vote
2 answers

PeriodicTimer with dynamic period in a BackgroundService

I'm using using PeriodicTimer timer = new(_period); while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken)) { await DoWork(); } Is there a way to set _period dynamically? that is, take that period…
Angelru
  • 150
  • 1
  • 13
0
votes
1 answer

Periodic Timer force to run every second

I create the periodic timer which run under background service public class PeriodicHostedService : BackgroundService { private readonly TimeSpan period = TimeSpan.FromSeconds(1); private readonly ILogger
0
votes
1 answer

Asynchronous operation suddenly stops?

So I have created this helper method to run a task every X ms, although the problem is the task suddenly stops executing after a few iterations and I can't understand why? No reason is given. Helper method: public static async Task…
C10
  • 21
  • 1
  • 4
0
votes
3 answers

Creating an awaitable System.Timers.Timer

I had an idea about creating a timer that can be awaited, instead of raising events. I haven't thought of any practical applications yet, and may not be something terribly useful, but I would like to see if it's at least doable as an exercise. This…
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
-1
votes
1 answer

PeriodicTimer running inside a Task

I have a top-level class that is a long-running management class for various requirements for my application. I need to have some kind of Timer set-up which will monitor any errors around my application, pausing a Quartz Scheduler execution for x…
Paul James
  • 121
  • 2
  • 7
-1
votes
1 answer

.NET 6.0 PeriodicTimer in Background Thread 300% cpu

var timer = new PeriodicTimer(TimeSpan.FromMinutes(5)); new Thread(async () => { try { while (await timer.WaitForNextTickAsync().ConfigureAwait(false)) { ... do some…
andrey1567
  • 97
  • 1
  • 13