为什么定时器没有工作

huerreson 2008-02-28 10:28:56

using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.ComponentModel;
using System.Web;
using System.Web.SessionState;
using System.Timers;
using System.Threading;
using System.Diagnostics;

namespace gift
{
/// <summary>
/// Global 的摘要说明。
/// </summary>
public class Global : System.Web.HttpApplication
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;

public Global()
{
InitializeComponent();
}

protected void Application_Start(Object sender, EventArgs e)
{
Pub.UpFileClass.WriteContentToFile(Pub.GetSet.SitePath+"log/"+ DateTime.Now.ToShortDateString() +".txt" ,"["+DateTime.Now+"] 系统开始运行.",true,"utf-8");
System.Timers.Timer myTimer = new System.Timers.Timer();
myTimer.Elapsed += new ElapsedEventHandler(myTimer_Elapsed);
myTimer.Interval = Pub.GetSet.AutoDelTimerInterval;
myTimer.AutoReset = true;
myTimer.Enabled = true;
Pub.UpFileClass.WriteContentToFile(Pub.GetSet.SitePath+"log/"+ DateTime.Now.ToShortDateString() +".txt" ,"["+DateTime.Now+"]"+myTimer.Interval,true,"utf-8");
}

........(省略若干代码)
private void myTimer_Elapsed(object source, ElapsedEventArgs e)
{
ThreadStart threadDelegat = new ThreadStart(DelExpiredBusiness);
Thread myThread = new Thread(threadDelegat);
if (Application["DelExpiredBusinessState"].ToString() != "Running")
{
myThread.Start();
}
}
/// <summary>
/// 删除过期信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void DelExpiredBusiness()
{
Application.Lock();
Application["DelExpiredBusinessState"] = "Running";
Application.UnLock();

SqlParameter[] param = new SqlParameter[]{new SqlParameter("@ExpiredDay",SqlDbType.Int),new SqlParameter("@RETURN_VALUE",SqlDbType.Int)};
param[0].Value = 90;
param[1].Direction = ParameterDirection.ReturnValue;
SqlDataReader rd = gift.Data.Provider.Global.getDataReader("sp_Business_expried_day",param);
while(rd.Read())
{
try
{
Pub.UpFileClass.DeleteFile(rd["BPic"].ToString());
Pub.UpFileClass.DeleteFile(rd["BHPic"].ToString());
}
catch
{

}
}
rd.Close();
gift.Data.Provider.Global.NonQuery("delete from business where DATEDIFF(DAY,BValidate,GETDATE())>90");
Application.Lock();
Application["DelExpiredBusinessState"] = "End";
Application.UnLock();
}
#region Web 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
}
#endregion
}
}

...全文
60 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
ljm66 2008-02-28
  • 打赏
  • 举报
回复
System.Timers.Timer timer 定义为全局变量就好了
johnwoo85 2008-02-28
  • 打赏
  • 举报
回复

void Application_Start(object sender, EventArgs e)
{
// 應用程式啟動時執行的程式碼
System.Timers.Timer timer = new System.Timers.Timer(1000 *60);
//AutoReset 属性为 true 时,每隔指定时间循环一次;
//如果为 false,则只执行一次。
timer.AutoReset = true;
timer.Enabled = true;
timer.Elapsed += new System.Timers.ElapsedEventHandler(timer_Elapsed);

}
System.Timers.Timer timer = new System.Timers.Timer(1000 *60);

62,047

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧