我在asp.net頁後面的代碼:如何正確使用Timer對象?
protected void LoadFile(object sender, EventArgs e)
{
try
{
Timer myTimer = new Timer();
myTimer.Elapsed += new ElapsedEventHandler(RemoveFile);
myTimer.Interval = 60000;
myTimer.Start();
}
catch (Exception)
{
}
}
private void RemoveFile(object source, ElapsedEventArgs e)
{
string path = UniquePath();
File.Delete(path);
}
當的LoadFile事件處理程序60發射秒,如果經過的LoadFile再次發射(因爲這行myTimer.Interval = 60000中所定義)之後的RemoveFile功能燒製40秒後,RemoveFile將在20秒內觸發。
我的問題是如何使RemoveFile
函數在上次調用LoadFile
事件處理程序60秒後激活?
'如果LoadFile在40秒後再次觸發,則RemoveFile將在20秒內觸發。「 - 您的意思是'RemoveFile'在那個時候會被調用兩次? (第一次通話後一分鐘,第二次後20秒)?或者它只會被調用一次? – Rob
請不要編寫'catch(Exception)',更不用說'catch(Exception){}'。這是一個隱藏錯誤的不良做法。 – Enigmativity
@Rob它只會被調用一次 – Michael