-1
我有一個循環,根據某些條件連續運行一個函數。 現在,我只想在循環內每10分鐘調用一次該函數。 我使用Visual Studio 2005中我的代碼是:如何在循環中使用c#每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();
}
嘗試Quartz.net作業調度程序。 http://stackoverflow.com/questions/8633662/call-a-method-at-a-certain-time 這是一個開源的 – Anand 2012-03-14 04:10:15
'aTimer.AutoReset = FALSE;如果你想'有你的問題 – Yaur 2012-03-14 04:31:04
無限期地執行此操作(即使在應用程序關閉後),可以考慮編寫一個應用程序來執行一次 - 即使是控制檯應用程序。然後在Windows中添加一個計劃任務,每10分鐘運行一次。 – 2012-03-28 14:39:32