定时器没有停止然后在执行方法后重新启动?

时间:2023-01-16 19:32:52

I have been working on this project for the past 3 days till 3-4 am and I believe I know where its messing up (My comments are marking the calculation that I believe is the issue) but as for solving the calculation and if that would even fix the timer I am not sure. I have marked in the code where I believe is the problem but I would accept any opinion on what they believe may fix it or where it is messing up. If it is the one calculation in the (Check) Method then I am not sure what calculation would fix it. If it is something else that someone else see's that I do not please tell me. I have looked at several different options and none seem to work.

过去3天我一直在研究这个项目,直到凌晨3点,我相信我知道它搞乱的地方(我的评论标志着我认为是问题的计算)但是至于解决计算,如果那样的话甚至修理计时器我不确定。我在代码中标记了我认为是问题的但我会接受任何关于他们认为可以解决它的问题或者它在哪里弄乱的意见。如果它是(检查)方法中的一个计算,那么我不确定什么计算会修复它。如果是别人看到的其他东西,我不会告诉我。我看了几个不同的选项,似乎都没有。

This project is meant to test a product, it is to send a message every (x) (sec, mins, hours, days) for (y) (sec, mins, hours, days). () being the users input. After the (x) is elapsed then its time to, stop timer, send method, then restart timer. (ex. (user enters) Send 1 message every (30) sec. for (2) min.) There should be 4 rounds of messages sent but it is ending early due to the timer not stopping. I have an idea of where it is messing up but a little guidance and other opinions would be helpful.

此项目旨在测试产品,它是每隔(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>

2 个解决方案

#1


0  

Why not do something like this in the Timer_Tick?

为什么不在Timer_Tick中做这样的事情?

If ticks = userTimeInterval Then
  SendMessage()
  Check()
  ticks = 0
Else
   ticks += 1
End If

Check the ticks (seconds) against what the user puts in to see if it is time to send the message, if it is then send it. Then check to see if it is time to stop sending the messages.

检查用户输入内容的滴答声(秒),看是否是发送消息的时间,如果是,则发送消息。然后检查是否该停止发送消息。

#2


0  

Before subtracting StartTime and ct make sure it is it the same fortmat as

在减去StartTime和ct之前,请确保它与它相同

string.Format("{0} days, {1} hours, {2} minutes, {3} seconds", te.Days, te.Hours,
                                        te.Minutes,
                                        te.Seconds);

#1


0  

Why not do something like this in the Timer_Tick?

为什么不在Timer_Tick中做这样的事情?

If ticks = userTimeInterval Then
  SendMessage()
  Check()
  ticks = 0
Else
   ticks += 1
End If

Check the ticks (seconds) against what the user puts in to see if it is time to send the message, if it is then send it. Then check to see if it is time to stop sending the messages.

检查用户输入内容的滴答声(秒),看是否是发送消息的时间,如果是,则发送消息。然后检查是否该停止发送消息。

#2


0  

Before subtracting StartTime and ct make sure it is it the same fortmat as

在减去StartTime和ct之前,请确保它与它相同

string.Format("{0} days, {1} hours, {2} minutes, {3} seconds", te.Days, te.Hours,
                                        te.Minutes,
                                        te.Seconds);