在过去的 3 天里,我一直在研究这个项目,直到凌晨 3 点到 4 点,我相信我知道它在哪里搞砸了(我的评论标记了我认为是问题的计算),但至于解决计算,如果那会甚至修复计时器我不确定。我已经在代码中标记了我认为是问题的地方,但我会接受任何关于他们认为可以解决问题或问题所在的意见。如果它是(检查)方法中的一个计算,那么我不确定哪种计算可以解决它。如果是其他人看到的我不喜欢的东西,请告诉我。我查看了几种不同的选项,但似乎都没有。
该项目旨在测试产品,它是每(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>