4

アプリでタイマーを実行していますが、現地時間に従って停止および開始したいと考えています。だから私はこのようなものが必要です:

if ( time = 08:00) {StartTimer();}
If ( time = 18:00) {StopTimer();} //This can be done from the timer event itself

別のタイマーを使用せずにこれを行う方法はありますか?タイマーイベント自体からタイマーを停止することはできますが、どのように再開しますか?

4

4 に答える 4

2

タイマーを停止したり、短い間隔で実行を続けて内部で追加の条件(時刻)をチェックしたりする代わりに、タイマーの間隔を14時間に設定できます。

于 2012-10-24T08:22:48.957 に答える
1

あなたはこれを試すことができます:-

1)探していることを実行するコンソールアプリを作成します。

2)Windowsの「スケジュールされたタスク」機能を使用して、実行する必要があるときにそのコンソールアプリを実行します。

また

この例も見ることができます:-

   using System;
   using System.Threading;

    public class TimerExample {

// The method that is executed when the timer expires. Displays
// a message to the console.
private static void TimerHandler(object state) {

    Console.WriteLine("{0} : {1}",
        DateTime.Now.ToString("HH:mm:ss.ffff"), state);
}

public static void Main() {

    // Create a new TimerCallback delegate instance that 
    // references the static TimerHandler method. TimerHandler 
    // will be called when the timer expires.
    TimerCallback handler = new TimerCallback(TimerHandler);

    // Create the state object that is passed to the TimerHandler
    // method when it is triggered. In this case a message to display.
    string state = "Timer expired.";

    Console.WriteLine("{0} : Creating Timer.",
        DateTime.Now.ToString("HH:mm:ss.ffff"));

    // Create a Timer that fires first after 2 seconds and then every
    // second.
    using (Timer timer = new Timer(handler, state, 2000, 1000)) {

        int period;

        // Read the new timer interval from the console until the
        // user enters 0 (zero). Invalid values use a default value
        // of 0, which will stop the example.
        do {

            try {
                period = Int32.Parse(Console.ReadLine());
            } catch {
                period = 0;
            }

            // Change the timer to fire using the new interval starting
            // immediately.
            if (period > 0) timer.Change(0, period);

        } while (period > 0);
    }

    // Wait to continue.
    Console.WriteLine("Main method complete. Press Enter.");
    Console.ReadLine();
}
}
于 2012-10-24T08:20:37.560 に答える
1

毎秒刻々と変化するスレッドを作成できます。

そこで、タイマーを開始するか停止するかを確認できます。

以下をお読みください:スレッド

スレッドに次のようなものを追加します。

if (CurrentTime == "08:00")
   StartTimer();
else if  if (CurrentTime == "18:00")
   StopTimer();
Thread.Sleep(1000); // Makes the Thread Sleep 1 Second
于 2012-10-24T08:21:39.263 に答える
1

少なくとも1つのタイマーを常に実行する必要があるため(午前8時を検出するため)、1日中実行するタイマーを1つだけにすることができます。

タイマーが作動するたびに、時間を確認してください。0800から1800の間にない場合は、何もせずに戻って次のティックを待ちます。

タイマー間隔をたとえば17:55になる値に増やしてから、もう一度減らすこともできますが、測定可能なパフォーマンスの違いはないため、これは何のメリットもありません。

于 2012-10-24T08:23:12.313 に答える