动态设置定时执行功能 [英] Execute function on timing set dynamically
问题描述
我有两个功能,如下所示:
I have two functions as following :
public void SendMessage()
{
//Some Code
}
AND
AND
public void CheckReceivedMessage()
{
//Some Code
}
现在,我要 CheckReceivedMessage(),此功能应连续运行,而 SendMessage(),此功能将每周运行一次,时间由用户设置.
我想为此使用线程.
例如,用户已将时间设置为星期三4:00 pm,然后每个星期三的4:00 pm,将执行 SendMessage(),如果今天是星期五&;用户更改时间和时间从周三到周日的一天,然后在即将到来的周日应该执行...
我已经为此尝试过....
Now, I want CheckReceivedMessage(), this function should run continuously and SendMessage(), this function will run once a week which time will be set by user.
I want to use Threading for this.
For example, the user has set time Wednesday, 4:00 pm then every wednesday at 4:00 pm, SendMessage() will be executed and if today is friday & the user changes time & day from Wednesday to Sunday, then Upcoming sunday it should be executed...
And I have tried this for it....
private void Form1_Load(object sender, EventArgs e)
{
ThreadStart thrdLoop = new ThreadStart(ThreadLoop);
Thread thrd1 = new Thread(thrdLoop);
thrd1.Start();
}
public void ThreadLoop()
{
while (true)
{
SendMessage();
System.Threading.Thread.Sleep(TimeSpan.FromDays(7));
}
}
但是在上面,当用户更改时间时,以为 SendMessage()将在上次执行后7天后执行...
请为此指导我...
谢谢...
But In above, when user will change time, thought SendMessage() will execute after 7 days after last execution...
Please guide me for this...
Thank you...
推荐答案
看看石英制调度器: http ://www.quartz-scheduler.net/ [^ ]
这是一种易于使用的通用调度程序,您不必为检查时间而编写逻辑.
希望对您有所帮助.
Have a look at quartz-scheduler: http://www.quartz-scheduler.net/[^]
It''s an easy to use generic scheduler and you don not have to write logic for your time checking.
Hope it helps.
线程解决方案不是一个好主意,因为当用户重新启动计算机时,时间将会浪费.我认为另一个最佳解决方案是使用Windows Task Scheduler.只需设置时间并选择要执行的操作即可.
Thread solution is not good idea because when user restarts the computer the time will be lost. I think another best solution will be use of Windows Task Scheduler. just set time and select what should be executed.
这篇关于动态设置定时执行功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!