標籤:
我用.net做的一個Timer定時器,定時擷取簡訊並給予回複,但大概過了十幾個小時以後,Timer定時器會自動停止,再傳送簡訊就不能收到回複,需要在伺服器中重新運行定時器才可以,請教各位!
我是在.net framework中的,有一個Global.asax全域應用程式檔案,帖代碼:
public class Global : System.Web.HttpApplication
{
double iTimerInterval;
System.Timers.Timer timer = new System.Timers.Timer();
SmsRun smsRun = new SmsRun();
object objLock = new object();
protected void Application_Start(object sender, EventArgs e)
{
//在應用程式啟動時啟動並執行代碼
//在新會話啟動時啟動並執行代碼
SetAccount();
timer.Start();//定時器開始
}
protected void Application_End(object sender, EventArgs e)
{
timer.Stop();
}
private void SetAccount()
{
double.TryParse(ConfigurationManager.AppSettings["TimerInterval"], out iTimerInterval);
timer.Interval = iTimerInterval;
timer.Elapsed += new System.Timers.ElapsedEventHandler(getMessage);
//getMessage是個方法(略)
}
提問者採納:
有可能是由於timer已經被回收掉了 你是在什麼環境下面winform? webform?
經過你的補充我看明白了 是這樣的 由於你的應用程式在特定空閑時間之後相應的w3wp輔助進程會被回收掉 所以你的timer自然沒有作用了 你可以查看iis 應用程式集區的屬性 切換到效能選項卡空閑逾時 預設的時間是20分鐘 也就是說如果20分鐘內你的網站沒有任何請求那麼就會造成回收
-----------------------------------------------------------
:
C#中WebService 的 Timer定時器過段時間後自動停止運行