过去3天我一直在研究这个项目,直到凌晨3点,我相信我知道它搞乱的地方(我的评论标志着我认为是问题的计算)但是至于解决计算和如果这甚至可以修复计时器,我不确定。我在代码中标记了我认为是问题但我会接受任何关于他们认为可能解决它的问题或者它在哪里弄乱的意见。如果它是(检查)方法中的一个计算,那么我不确定什么计算会修复它。如果是别人看到的其他东西,我不会告诉我。我看了几个不同的选项,但似乎都没有。
此项目旨在测试产品,它是每隔(x)(秒,分钟,小时,天)发送(y)(秒,分钟,小时,天)的消息。 ()是用户输入。经过(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>
答案 0 :(得分:0)
在减去StartTime和ct之前,请确保它的格式与
相同string.Format("{0} days, {1} hours, {2} minutes, {3} seconds", te.Days, te.Hours,
te.Minutes,
te.Seconds);
答案 1 :(得分:0)
为什么不在Timer_Tick中做这样的事情?
If ticks = userTimeInterval Then
SendMessage()
Check()
ticks = 0
Else
ticks += 1
End If
检查用户输入内容的滴答声(秒),看是否是发送消息的时间,如果是,则发送消息。然后检查是否该停止发送消息。