1

私は過去 3 日間、午前 3 時から 4 時までこのプロジェクトに取り組んでおり、どこがめちゃくちゃになっているのかはわかっていると思います (私のコメントは、私が問題であると信じている計算をマークしています)。よくわからないタイマーを修正することさえできます。問題があると思われるコードにマークを付けましたが、それを修正できると思われるものや、どこがめちゃくちゃになっているのかについての意見を受け入れます。それが (チェック) メソッドの 1 つの計算である場合、どの計算がそれを修正するのかわかりません。それが他の誰かが見ているものである場合は、私に教えてください. 私はいくつかの異なるオプションを見てきましたが、どれもうまくいかないようです。

このプロジェクトは製品をテストするためのもので、(x) (秒、分、時間、日) ごとにメッセージを (y) (秒、分、時間、日) 送信します。() はユーザー入力です。(x) が経過した後、タイマーを停止し、メソッドを送信し、タイマーを再起動します。(例: (ユーザーが入力) 1 メッセージを (30) 秒ごとに (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>
4

2 に答える 2

0

Timer_Tick でこのようなことをしてみませんか?

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

ユーザーが入力した内容に対してティック (秒) をチェックして、メッセージを送信する時間であるかどうかを確認します。次に、メッセージの送信を停止する時期かどうかを確認します。

于 2012-08-13T16:47:18.213 に答える
0

StartTime と ct を減算する前に、次の形式と同じであることを確認してください。

string.Format("{0} days, {1} hours, {2} minutes, {3} seconds", te.Days, te.Hours,
                                        te.Minutes,
                                        te.Seconds);
于 2012-08-13T16:44:18.180 に答える