Click here to Skip to main content
15,910,210 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
In C# in .NET Core Web App, I want to trigger a method to execute after a specific amount of time (seconds / minutes).

I need to "schedule" a large number of such methods executions (500-1000).

For example, below:

DoProcess(1) should execute in 1 seconds, DoProcess(2) in 2 seconds, DoProcess(3) in 3 seconds , etc...
DoProcess(2) should NOT to wait for DoProcess(1) to finish.

C#
public async Task ProcessUsers()
{
    for (var delay = 1; delay < 500; delay++)
    {
        Task.Delay(delay).ContinueWith(async task =>await DoProcess(delay));
    }
}
    
public async Task DoProcess(int val)
{
    // Do some work that takes a few seconds.
    // work 123
}


1. Will that code work as I described?
2. Will each Task.Delay() create a new thread and (due to the large number of executions, 500) cause thread pool overload/exhaustion?
3. Can you suggest a better way to handle such requirement? I considered using some library like Quartz or Hangfire, but that seemed like an overkill.

What I have tried:

I provided above the code i wrote.
Posted
Comments
Rob Philpott 21-May-24 12:49pm    
Task.Delay takes the delay in milliseconds by the way, not seconds.

For .Net Core, you would use the BackgroundService[^] class. You can read more about it here: Background tasks with hosted services in ASP.NET Core | Microsoft Learn[^].

If you want to see it in action, I have a couple of articles where I use the BackgroundService[^] class:
* .NET Silent ClickOnce Installer for Winform & WPF in C# & VB[^]
* LogViewer Control for WinForms, WPF, and Avalonia in C# & VB[^]

If you do a Google[^] or YouTube[^] search, you will find many more examples.

Quote:
Will each Task.Delay() create a new thread and (due to the large number of executions, 500) cause thread pool overload/exhaustion?

No, Async/Await uses the ThreadPool. The ThreadPool is a managed resource, so you won't exhaust it. Worse case, on a busy sight, it will simply queue the Tasks.
 
Share this answer
 
v2
Comments
RuSho123 20-May-24 5:44am    
The above code is already in BackgroundService. in that service we need to trigger calls to DoProcess multiple times , each time a with different delay before calling the DoProcess.

If current time is 14:00:00

DoProcess(1) - should execute at 14:00:50.
DoProcess(2) - should execute at 14:00:20.
DoProcess(3) - should execute at 14:05:20.
DoProcess(4) - should execute at 14:03:00.
etc...
Graeme_Grant 21-May-24 6:20am    
A background service can do what you want, you simply have to code it. You have a central ticker and that executes the jobs based on your custom job scheduler. What that is is up to you to create.

If that is too much, then look at third-party like libs. Watch this: The Easiest Scheduling for Your .NET Applications - YouTube[^]
A bigger design question should be do these methods have to run while the site is running? For example, database maintenance jobs? If not, don't even put them into the website code.

Write another app, like a Windows Service, that does the maintenance and separate the concerns. The web app should be concerned with nothing but presenting the site to the users. Maintenance should be external to that and handled by a separate app.
 
Share this answer
 
Comments
RuSho123 20-May-24 5:45am    
Yes it needs to happen when the site is running.
DoProcess is a short DB access and logic method.
Dave Kreskowiak 20-May-24 10:07am    
You might get away with this if you don't need less than 1 second accuracy. The BackgroundService would have to execute once a second and check a list of scheduled times to see if the current time is in the list. If it is, execute the method attached to that time.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900