积极答复者
怎么设置一个程序在每天的定时运行一次?

问题
-
在asp。net的网站里面,怎么设置一个页面,每天定时比如6点执行某个程序?
- 已移动 孟宪会Moderator 2009年8月10日 3:21 (发件人:.NET Framework 一般性问题讨论区)
答案
-
ASP.NET 开发中我们也许会遇到在服务期定时运行某些功能的需求,不要烦恼.NET为我们提供了很好的解决方案。下面我们通过一个比较简单的示例来说明一下。
我们新建一个 WEB 项目,然后在项目中添加“全局应用程序类(Global.asax)”,代码如下:
/**/ /// <summary>
/// 定义时钟。
/// </summary>
private System.Timers.Timer m_timer;
void Application_Start( object sender, EventArgs e)
... {
// 在应用程序启动时运行的代码
m_timer = new System.Timers.Timer( 1000 );
m_timer.Enabled = true ;
m_timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timer_Elapsed);
// 开启时钟。
m_timer.Start();
}
/**/ /// <summary>
/// 时钟定时执行函数。
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void m_timer_Elapsed( object sender, System.Timers.ElapsedEventArgs args)
... {
// your action here
object obj = this .Application[ " _TimeRun " ];
int Num = 0 ;
if (obj != null )
... {
Num = Convert.ToInt32(obj);
}
Num ++ ;
this .Application[ " _TimeRun " ] = Num;
}
测试代码如下(ASPX):
< asp:TextBox ID ="m_edtTimeNum" runat ="server" ></ asp:TextBox >
< asp:Button ID ="m_btnGetTimeNum" runat ="server" OnClick ="m_btnGetTimeNum_Click" Text ="获取定时器计数" /></ div >
测试代码如下(CS):
protected void m_btnGetTimeNum_Click( object sender, EventArgs e)
... {
this .m_edtTimeNum.Text = Convert.ToString( this .Application[ " _TimeRun " ]);
}
- 已标记为答案 wanguan2000 2009年8月10日 3:31
全部回复
-
你好 你可以查看 一些论坛程序的任务模块
一般是在后台有个线程一直监控,对满足条件时 则执行相应的方法
比如每隔1个小时 清除 缓存文件
每隔1个小时重新统计积分这些
Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond -
ASP.NET 开发中我们也许会遇到在服务期定时运行某些功能的需求,不要烦恼.NET为我们提供了很好的解决方案。下面我们通过一个比较简单的示例来说明一下。
我们新建一个 WEB 项目,然后在项目中添加“全局应用程序类(Global.asax)”,代码如下:
/**/ /// <summary>
/// 定义时钟。
/// </summary>
private System.Timers.Timer m_timer;
void Application_Start( object sender, EventArgs e)
... {
// 在应用程序启动时运行的代码
m_timer = new System.Timers.Timer( 1000 );
m_timer.Enabled = true ;
m_timer.Elapsed += new System.Timers.ElapsedEventHandler(m_timer_Elapsed);
// 开启时钟。
m_timer.Start();
}
/**/ /// <summary>
/// 时钟定时执行函数。
/// </summary>
/// <param name="sender"></param>
/// <param name="args"></param>
private void m_timer_Elapsed( object sender, System.Timers.ElapsedEventArgs args)
... {
// your action here
object obj = this .Application[ " _TimeRun " ];
int Num = 0 ;
if (obj != null )
... {
Num = Convert.ToInt32(obj);
}
Num ++ ;
this .Application[ " _TimeRun " ] = Num;
}
测试代码如下(ASPX):
< asp:TextBox ID ="m_edtTimeNum" runat ="server" ></ asp:TextBox >
< asp:Button ID ="m_btnGetTimeNum" runat ="server" OnClick ="m_btnGetTimeNum_Click" Text ="获取定时器计数" /></ div >
测试代码如下(CS):
protected void m_btnGetTimeNum_Click( object sender, EventArgs e)
... {
this .m_edtTimeNum.Text = Convert.ToString( this .Application[ " _TimeRun " ]);
}
- 已标记为答案 wanguan2000 2009年8月10日 3:31