在asp.net forums2 或者 cs 系統中, 每過幾分種會發送一次email, 但是如果在執行時,數據庫服務器重啟。
會導致Send Email 線程死掉。
參考代碼:
PRivate void ScheduledWorkCallbackEmailInterval (object sender) 
{
   try {
    // suspend the timer while we process emails
    emailTimer.Change( System.Threading.Timeout.Infinite, EmailInterval );
    // Send emails
    //
    Emails.SendQueuedEmails( (HttpContext) sender);
    // Update anonymous users
    //
    Users.UpdateAnonymousUsers( (HttpContext) sender);
   }
   catch( Exception e ) {
    ForumException fe = new ForumException( ForumExceptionType.EmailUnableToSend, "Scheduled Worker Thread failed.", e );
    fe.Log();
   }
   finally {
    emailTimer.Change( EmailInterval, EmailInterval );
   }
        }
事實上,代碼:emailTimer.Change( System.Threading.Timeout.Infinite, EmailInterval );
不夠強壯,理論上講,如果在執行中出錯,會執行:
finally {
    emailTimer.Change( EmailInterval, EmailInterval );
   }
但,事實上,如果是數據庫服務器重啟,則可以讓timer線程永遠死掉。
手工解決方法: 重啟這個web app
或者改寫代碼:emailTimer.Change( System.Threading.Timeout.Infinite, EmailInterval );
為:emailTimer.Change( EmailInterval * 2, EmailInterval );
可以解決
其他參考:
msdn:
System.Theading.Timer.Change API:
新聞熱點
疑難解答