2012-03-14 15 views
-1

私はある条件に基づいて関数を連続して実行するループを持っています。 今、ループ内で10分ごとにその関数を呼び出す必要があります。私は、Visual Studioに2005私のコードを使用しています は次のとおりです。ループを使って10分ごとに関数を実行するには?

while (boolValue == false) 
    { 
     Application.DoEvents(); 
     StartAction(); //i want to call this function for every 10 minutes only 
    } 

私はSystem.Timersを使用していますが、それは関数を呼び出していません。私は何が間違っているのか分からない。

私のコードは次のとおりです。

public static System.Timers.Timer aTimer; 
    while (boolValue == false) 
    { 
     aTimer = new System.Timers.Timer(50000); 
     aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); 
     aTimer.AutoReset = false; 
     aTimer.Enabled = true; 
    } 

    private static void OnTimedEvent(object source, ElapsedEventArgs e) 
    { 
     Application.DoEvents(); 
     StartAction(); 
    } 
+0

Quartz.netジョブスケジューラを試してみてください。 http://stackoverflow.com/questions/8633662/call-a-method-at-a-certain-time それはオープンソース – Anand

+0

'aTimer.AutoReset = falseのだ。あなたがしたい場合は、'あなたの問題 – Yaur

+0

がありますこれを無期限に(アプリが終了した後でも)実行してください。アプリを作成することを検討してください。次に、Windowsでスケジュールされたタスクを追加して10分ごとに実行します。 –

答えて

5

はなぜちょうどtimerを使用していません。それは10分ごとに起動します。あなたの更新されたコードで

より特定のバージョンでexampleはかなり良い例です。実際に

UPDATE

、私はこのように変更します:

public static System.Timers.Timer aTimer; 
... 
aTimer = new System.Timers.Timer(50000); 
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent); 
aTimer.AutoReset = false; //This should be true if you want it actually looping 
aTimer.Enabled = true; 

私にはありませんwhileループを持つ理由を参照してください。私の推測では、whileループはまったくトリガーされていないということです。また、AutoResetをtrueに設定して、これが継続的に実行されるようにする必要があります。

関連する問題