我用.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分鍾內你的網站沒有任何請求那么就會造成回收
-----------------------------------------------------------
截圖:
