简体   繁体   English

一个应用程序(.Net C#)中的计划任务

[英]Scheduled tasks in one application (.Net C#)

I got a database of subscriptions, I want to run a for every subscriptions from one time to another every x minute. 我有一个订阅数据库,我想每x分钟为每个订阅运行一次。

But every subscription has its own from and to time + what every x minutes its should run the function. 但是每个订阅都有自己的时间和时间+每x分钟应运行的功能。

Its can be many subscriptions running the same time. 它可以是同时运行的多个订阅。

Maybe its should looks like a scheduled task that starting at one specific time and runs every x minute to one specific time. 也许它应该看起来像一个计划任务,该任务从一个特定时间开始,每x分钟运行到一个特定时间。

I hope I describe what I want to do right, my English is not the best. 我希望我描述自己想做的正确的事,我的英语不是最好的。

Do anyone has any idea how I building the application todo this? 有谁知道我如何构建应用程序来做到这一点?

You have a couple of choices here: 您可以在这里选择两个:

  • Use the SQL Agent and setup jobs with the required schedule (of course, this assumes you are using SQL Server). 将SQL Agent和设置作业与所需的计划一起使用(当然,这是假定您正在使用SQL Server)。
  • Write a Windows Service with a timer that queries your database and executes the different tasks as needed. 编写一个带有计时器的Windows服务 ,该计时器查询您的数据库并根据需要执行不同的任务。

You mentioned scheduled tasks , which might be another option, but that will require you to automate the setting up of new tasks as well as the actual tasks to run. 您提到了计划任务 ,这可能是另一个选择,但这将要求您自动设置新任务以及要运行的实际任务。 Both of these are easier to do with the options I have outlined above. 使用我上面概述的选项,这两个选项都更容易实现。

I would recommend you to use System.Threading.Timer for that. 我建议您为此使用System.Threading.Timer To schedule a job, do the following: 要安排工作,请执行以下操作:

new Timer(_ => DoTheJob()).Change(your_interval_in_milliseconds, Timeout.Infinite);

then when job is done, schedule next run using the same code. 然后在完成工作后,使用相同的代码安排下一次运行。 That would prevent running 2 instances of the same job simultaneously. 这样可以防止同时运行同一作业的2个实例。 Please let me know if that does not answer your question appropriately. 如果那不能正确回答您的问题,请告诉我。

You are maybe describing just what Quartz.NET can do for you. 您可能正在描述Quartz.NET可以为您做什么。 Check it out, it might be worth spending some time. 检查一下,可能值得花一些时间。

So you should write some kind of scheduler. 因此,您应该编写某种调度程序。 It will take tasks from the queue and execute them... You can create a scheduler in such a way that every task for example will run in a separate thread and it would significantly improve performance! 它将从队列中取出任务并执行它们...您可以以一种方式创建调度程序,例如,每个任务都将在单独的线程中运行,这将显着提高性能!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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