[英]Timer won't start after stop
In my application I'm using two Timer
, each Timer
use a BackgroundWorker
. 在我的应用程序中,我使用两个Timer
,每个Timer
使用BackgroundWorker
。 Here the declaration: 这里的声明:
DispatcherTimer timer1 = new DispatcherTimer();
DispatcherTimer timer2 = new DispatcherTimer();
BackgroundWorker worker1 = new BackgroundWorker();
BackgroundWorker worker2= new BackgroundWorker();
I using timer1
for perform an heavy method with a BackgroundWorker
and timer2
for execute another BackgroundWorker
that check the content of a file. 我使用timer1
与BackgroundWorker
一起执行繁重的方法,并使用timer2
执行另一个检查文件内容的BackgroundWorker
。
In this way I assign the event to BackgroundWorkers
: 通过这种方式,我将事件分配给BackgroundWorkers
:
worker1.DoWork += worker_DoWork;
worker1.RunWorkerCompleted += worker_RunWorkerCompleted;
worker1.WorkerSupportsCancellation = true;
worker2.DoWork += worker_DoWork2;
worker2.RunWorkerCompleted += worker_RunWorkerCompleted2;
worker2.WorkerSupportsCancellation = true;
Now timer1
have a range of 15 minutes
so the BackgroundWorker
execute the heavy method each 15 minutes. 现在timer1
的范围为15 minutes
因此BackgroundWorker
每15分钟执行一次重方法。 And timer2
have a range of 1 second
. 并且timer2
的范围为1 second
。 With the timer1
all working good, but the problems are coming when I've added the timer2
. 在timer1
一切正常,但是当我添加了timer2
时,问题就来了。
As I said before this timer allow me to start a method that read a file through the worker2
, this file have a property, if this property change I need to perform some special activity. 正如我在此计时器之前所说的那样,允许我启动一个通过worker2
读取文件的worker2
,该文件具有一个属性,如果此属性更改,则需要执行一些特殊的活动。 Until here no problem. 到这里为止没有问题。
What I did is the following: 我所做的是以下几点:
//This method is called by MainWindow
public ReadFile()
{
//before this I already assigned to timer1 the tick event and start
timer2.Tick -= new EventHandler(Event_Tick);
timer2.Tick += new EventHandler(Event_Tick);
timer2.Interval = new TimeSpan(0, 0, 1);
timer2.Start();
}
This is the Tick
event associated to timer2
这是与timer2
相关的Tick
事件
private void Event_Tick(object sender, EventArgs e)
{
if (!worker1.IsBusy) //I skip the reading, worker1 is busy
{
timer1.Stop(); //stop the first timer
worker2.RunWorkerAsync();
}
else
{
Console.WriteLine("worker1 is busy!");
}
}
I don't need to add here the DoWork
, is just a parsing of a file, very useless for the question. 我不需要在此处添加DoWork
,它只是一个文件的解析,对于这个问题来说非常没用。 When worker2
complete the task I did this: 当worker2
完成任务时,我这样做:
private void worker_RunWorkerCompleted2(object sender, RunWorkerCompletedEventArgs e)
{
timer1.Start();
ReadFile();
}
How you can see I start the timer1
again, and execute again the ReadFile
method. 您如何看到我再次启动timer1
,并再次执行ReadFile
方法。 Now if timer1
has reached the interval, so 15 minutes
has passed, should execute the timer1.Tick += new EventHandler(Heavy_Tick);
现在,如果timer1
已达到该间隔, timer1.Tick += new EventHandler(Heavy_Tick);
经过了15 minutes
,应执行timer1.Tick += new EventHandler(Heavy_Tick);
that execute the DoWork
to worker1
. 执行对worker1
的DoWork
。 But the timer1
never start. 但是timer1
永远不会启动。
I can't figure out to this, what am I doing wrong? 我不知道这是怎么回事?
So a lot could be going on here but you should make sure that: 因此,这里可能发生了很多事情,但是您应该确保:
You timer isn't storing it's old progress and you are checking for a certain length of time before stopping. 您的计时器没有存储它的旧进度,并且您正在检查特定时间长度,然后再停止。 This will automatically cause the timer to stop when restarting. 重新启动时,这将自动导致计时器停止。
The timer.stop() function is not disposing your object to an un-restart-able state. timer.stop()函数不会将对象置于无法重新启动的状态。
You aren't accessing the timer variable through some pointer that is maintain a stopped value. 您不会通过某个保持停止值的指针来访问计时器变量。 (Unlikely but annoying when it happens) (不太可能,但是很烦人)
I'd personally consider just pausing the timer and resetting the progress, instead of fully stopping it since this is causing issues. 我个人认为只是暂停计时器并重置进度,而不是完全停止它,因为这会引起问题。
Now I get it! 现在我懂了! You want to execute worker1
every 15 minutes and worker2
every second but only when worker1
is not busy. 您希望每15分钟执行一次worker1
每秒执行一次worker2
但仅当worker1
不忙时才worker1
。 Your problem is this here: 您的问题是这里:
if (!worker1.IsBusy) //I skip the reading, worker1 is busy
{
timer1.Stop(); //stop the first timer
worker2.RunWorkerAsync();
}
and this: 和这个:
public ReadFile()
{
//before this I already assigned to timer1 the tick event and start
timer2.Tick -= new EventHandler(Event_Tick);
timer2.Tick += new EventHandler(Event_Tick);
timer2.Interval = new TimeSpan(0, 0, 1);
timer2.Start();
}
Set both timer intervals and tick event handlers during startup, eg Form_Load()
or at the beginning of Main()
. 在启动期间设置计时器间隔和滴答事件处理程序,例如Form_Load()
或Main()
的开始。 Start them there too. 也从那里开始。 You should not have to stop any timer at all! 您完全不必停止任何计时器! By setting the interval, all you have to do is handle the Tick()
event. 通过设置间隔,您要做的就是处理Tick()
事件。 Remove your .Start()
and Stop()
calls from your WorkerCompleted
and Tick
methods and you should do fine. 从WorkerCompleted
和Tick
方法中WorkerCompleted
.Start()
和Stop()
调用,您应该做得很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.