如果有定期需要發信或是備份等排程工作,可以開新專案用 WindowService來做,除此之外,這裡用一個偷懶的方式,就是直接在global的Application_Start加入timer 做排程,更進階的排程framework可以參考 ASP.NET 程式中的背景工作 (3) - 使用 Quartz.NET
以下展示簡易的方法 :
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protected void Application_Start() | |
{ | |
// 定時排程 | |
Timer t = new Timer(3600000 * during); //設計時間間隔,如果一個小時執行一次就改為3600000 | |
t.Elapsed += new ElapsedEventHandler(t_Elapsed); | |
t.AutoReset = true; | |
t.Enabled = true; | |
} | |
private void t_Elapsed(object sender, ElapsedEventArgs e) | |
{ | |
//排程內容 | |
} |