简体   繁体   中英

How do I create a timed scheduler in asp.net MVC?

I would like to create a relatively simple timed event scheduling function in an MVC website for application functions, not necessarily for users at this time, but could be in the future. My current desire is to use the schedule to cause specific changes to the website based on timed events. The general idea is shown below at the end of the post.

I first investigated Quartz for asp.net because it was recommended on other posts, but the advice in that open source package specifically recommended not using it in an embedded mode. I don't think I can add it as a service to the server where the MVC site is hosted.

Although I'm familiar with the concept of multi-threading, I am not experienced with it. Is running the Timer Process below on a background thread a good solution, or should I be looking at something else? I assumed the Timer Process would simply execute forever, comparing the schedule against the system clock and cause the required event code to run when the time trigger occurs.

If threading is a bad idea please advise where I should look. If threading is the right idea, any suggestions or warnings would be appreciated. I am reading up on threading at this time.

I have several specific questions:

1) Should the individual event code run on the same thread, or does it matter? 
2) How do I handle the problem others have suggested occurs if 'IIS kills the thread' or the 'app pool is recycled'.
3) Is it a wrong idea that I send some message to the MVC site that triggers the Timer Process to overcome the issues in #2 above?
4) Is there a way to force a page to automatically refresh with a new display when the timer event occurs?  That doesn't seem to be the way asp.net MVC normally works.

Finally, this sounded like a problem that others have probably resolved and reduced to an open source solution for asp.net MVC. As I look through Nu-Get there are a few that seem to describe this problem, but they have very few downloads. Any recommendations along this route?

在此处输入图片说明

I think in your case will be good to use Hangfire. You can configure like parallel server and create different jobs with different schedule.

For NetCore app:

app.UseHangfireServer();
app.UseHangfireDashboard("/hangfire", options);

// Process notifications every 6 hours
RecurringJob.AddOrUpdate<B2CEngineNotifications>(jobEngine =>  jobEngine.SendBillingNotifications(), "0 */6 * * *");

In this case each job works in his own thread.

1) Should the individual event code run on the same thread, or does it matter? It depends what type of events do you expect. If they are not depended, you can do it in separate threads.

How do I handle the problem others have suggested occurs if 'IIS kills the thread' or the 'app pool is recycled'. Hangfire will create in your DB some tables. If you will terminate process, on next start it get all data from db and start execute events on scedule.

Is it a wrong idea that I send some message to the MVC site that triggers the Timer Process to overcome the issues in #2 above? Hangfire has his own interface to manage jobs. Or you need to have a option to start event from your MVC app? You can do it from MVC just change db values. And hangfire will start job.

Is there a way to force a page to automatically refresh with a new display when the timer event occurs? That doesn't seem to be the way asp.net MVC normally works. In this case you need to use AJAX to have "live" connection with server and "ask" is something changed or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM