Home >Backend Development >C++ >How Can I Run Asynchronous Methods at Regular Intervals in C#?
C#mid -term execution asynchronous method
This article provides a solution to the service to the service from the C# web application with an asynchronous method and the
cycle.
while
The limitations of the Timer class
The class that is usually used for planning tasks, accepting the signature
method. This will bring restrictions when trying to execute asynchronous methods to call Web services.
Timer
asynchronous task cycle void MethodName(object e, ElapsedEventArgs args)
In order to overcome this limit, you can use cycle binding :
while
This cycle will be regularly executed by the Task.Delay
method, and uses the specified interval and the cancellation token. You can stop the cycle at any time by passing the tokens from Web applications.
<code class="language-csharp">public async Task PeriodicFooAsync(TimeSpan interval, CancellationToken cancellationToken) { while (!cancellationToken.IsCancellationRequested) // 使用CancellationToken改进 { await FooAsync(); await Task.Delay(interval, cancellationToken); } }</code>Other precautions
FooAsync
The above is the detailed content of How Can I Run Asynchronous Methods at Regular Intervals in C#?. For more information, please follow other related articles on the PHP Chinese website!