定时器没有停止,然后在执行方法后重新启动?
问题描述:
我一直在这个项目上工作了3天,直到上午3-4点,我相信我知道它在哪里搞乱(我的意见是标记计算,我相信是问题),但至于解决计算,如果那甚至会修复我不确定的计时器。我在代码中标明了我认为是问题的地方,但我会接受他们认为可以修复它的地方或它搞乱的地方的任何意见。如果它是(检查)方法中的一个计算,那么我不确定什么计算可以修复它。如果别人看到别的东西,我不想告诉我。我看了几个不同的选项,似乎没有任何工作。 (y)(秒,分钟,小时,天)每隔(x)(秒,分钟,小时,天)发送一条消息。 ()是用户的输入。 (x)过后,它的时间到,停止定时器,发送方法,然后重新启动定时器。 (例如(用户输入)每隔(30)秒发送1条消息(2)分钟)应该发送4轮消息,但由于定时器不停止而提前结束。我知道它在哪里搞乱了,但有一点指导意见和其他意见会有帮助。定时器没有停止,然后在执行方法后重新启动?
<code>
private void TimeKeeperTimerElapsed(object sender, ElapsedEventArgs e)
{
if (flag == true && EndTime > DateTime.Now)
{
_timeKeeper.Stop();
Check();
_timeKeeper.Start();
}
if (flag == true && EndTime < DateTime.Now)
{
TestCompleted();
}
}
bool flag = true;
/// <summary>
/// Method "Check" checks to see it it is time to send a message, time elapsed and time left
/// </summary>
private void Check()
{
if (SelectedPerfTest != null && flag == true)
{
//Est. Date Time Now
var ct = DateTime.Now;
//Time Elapsed Output form now (counts up)
var te = ct.Subtract(StartTime);
TimeElapsed = string.Format("{0} days, {1} hours, {2} minutes, {3} seconds", te.Days, te.Hours,
te.Minutes,
te.Seconds);
// Time Left from now output (counts down)
var tl = EndTime.Subtract(ct);
TimeLeft = string.Format("{0} days, {1} hours, {2} minutes, {3} seconds", tl.Days, tl.Hours,
tl.Minutes,
tl.Seconds);
//Calculate the time til the next message (counting down)
var nnm = CalculateNextMessage(DateTime.Now);
NextNewMessage = string.Format("{0} days, {1} hours, {2} minutes, {3} seconds", nnm.Days, nnm.Hours,
nnm.Minutes,
nnm.Seconds);
//I feel like the Problem is here this equation will call the method to send message but the timer continues to tick... I
//need it to stop when the time elapsed that the user put in ex. user enter (send 1 message every (20 seconds)
//for (2 hours)) it needs to stop in 20 sec. to send message then check if its time to end (if 2 hours are up).
//If not send message in another 20 sec.
if (DateTime.Now.Add(CalculateNextMessage(DateTime.Now)) < DateTime.Now && EndTime > DateTime.Now)
if (ct.Subtract(StartTime) == LastExecuted.AddMilliseconds(ts).Subtract(ct))
{
if (CurrentProduct == ("Babyware"))
{
if (SelectedPerfTest == ("Short Test"))
{
ExecuteShortTest();
}
if (SelectedPerfTest == ("Long Test"))
{
ExecuteLongTest();
}
if (SelectedPerfTest == ("UCN"))
{
ExecuteUCNTest();
}
}
else
if (CurrentProduct == ("Antware"))
{
if (SelectedPerfTest == ("Short Test"))
{
ExecuteGAShortTest();
}
if (SelectedPerfTest == ("Long Test"))
{
ExecuteGALongTest();
}
}
}
}
}
#endregion
</code>
答
为什么不这样做的Timer_Tick这样的事情?
If ticks = userTimeInterval Then
SendMessage()
Check()
ticks = 0
Else
ticks += 1
End If
检查用户放入的时间(秒),看看是否是时候发送邮件,然后发送邮件。然后检查是否该停止发送消息。
答
减去开始时间之前和CT确保它是相同fortmat作为
string.Format("{0} days, {1} hours, {2} minutes, {3} seconds", te.Days, te.Hours,
te.Minutes,
te.Seconds);
+0
我仔细检查了开始时间,并且格式正确。感谢您的建议。 – 2012-08-13 17:45:06
请限制您的代码示例到相关部分。理想的示例是[简短,独立和可编译](http://sscce.org/)。通读整个源代码非常耗时,并且会为您提供更少的答案。 – 2012-08-13 16:16:43
如果您的'Check'例程引发异常,并且您的计时器类型为'Threading.Timer',它将绕过'Start()'并且有时不会通知您该异常。也许在'try/finally'中包装'Check()'并将'Start()'调用移至'finally'块可能会有帮助。 – 2012-08-13 16:55:04
我确实试过你的建议,但定时器仍然不会停止发送消息。谢谢。 – 2012-08-13 17:27:23