ASP.NET 中使用计时器执行用户代码
2006-12-30 14:07
441 查看
ASP.NET 中使用计时器执行用户代码
ASP.NET 中使用计时器执行用户代码 首先引入命名空间
using System.Timers;
using System.IO;
在 Global.asax 中加入静态对象(变量)
public static Timer gtimer = null; // 定义全局定时器类对象
public static int gcount = 0; // 测试用计时器变量
在 Global.asax 中的 Application_OnStart 事件过程中定义计时器,代码如下:
protected void Application_Start(Object sender, EventArgs e)
{
// 创建一个计时器,单位:毫秒
gtimer = new Timer(1000);
// 将自定义用户函数(TimerEventFunction)指定为计时器的 Elapsed 事件处理程序
// TimerEventFunction 可以写入自己的需要执行的代码逻辑
gtimer.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerEventFunction);
// AutoReset 属性为 true 时,每隔指定时间间隔触发一次事件
// 若赋值 false,则只执行一次
gtimer.AutoReset = true;
gtimer.Enabled = true;
}
TimerEventFunction 函数代码如下:
// 定时器定时触发控件
protected void TimerEventFunction(Object sender, ElapsedEventArgs e)
{
// 这里写入自定义代码
gcount += 1;
FileInfo file = new FileInfo("C://" + gcount.ToString() + ".txt");
file.Create(); // 创建文件,定时执行此过程将不停的创建文件
}
ASP.NET 中使用计时器执行用户代码 首先引入命名空间
using System.Timers;
using System.IO;
在 Global.asax 中加入静态对象(变量)
public static Timer gtimer = null; // 定义全局定时器类对象
public static int gcount = 0; // 测试用计时器变量
在 Global.asax 中的 Application_OnStart 事件过程中定义计时器,代码如下:
protected void Application_Start(Object sender, EventArgs e)
{
// 创建一个计时器,单位:毫秒
gtimer = new Timer(1000);
// 将自定义用户函数(TimerEventFunction)指定为计时器的 Elapsed 事件处理程序
// TimerEventFunction 可以写入自己的需要执行的代码逻辑
gtimer.Elapsed += new System.Timers.ElapsedEventHandler(this.TimerEventFunction);
// AutoReset 属性为 true 时,每隔指定时间间隔触发一次事件
// 若赋值 false,则只执行一次
gtimer.AutoReset = true;
gtimer.Enabled = true;
}
TimerEventFunction 函数代码如下:
// 定时器定时触发控件
protected void TimerEventFunction(Object sender, ElapsedEventArgs e)
{
// 这里写入自定义代码
gcount += 1;
FileInfo file = new FileInfo("C://" + gcount.ToString() + ".txt");
file.Create(); // 创建文件,定时执行此过程将不停的创建文件
}
相关文章推荐
- 在 ASP.NET 中使用计时器执行用户代码(原创代码)
- ASP.NET提示框,且根据用户选择执行不同代码的实现
- ASP.NET在后台代码实现个功能,根据选择提示用户是否继续执行操作
- 使用Asp.net Identity 创建用户 、登录代码
- 【asp.net 】使用js结合hidden控件实现在后台运行一段程序后提示确认,确认通过后继续执行后台代码~~~
- 如何使用ASP.NET代码查看、修改AD用户信息
- 七天学会ASP.NET MVC (四)——Layout页面使用和用户角色管理 (代码下载)
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
- 在asp.net里使用指定的用户运行访问Sharepoint网站的代码
- 使用ASP.NET AJAX无刷新验证用户的代码
- 每日学习心得:CustomValidator验证控件验证用户输入的字符长度、Linq 多字段分组统计、ASP.NET后台弹出confirm对话框,然后点击确定,执行一段代码
- 使用ASP.NET中关于代码分离的实例分享
- 使用JavaScript调用asp.net代码
- [ASP.NET][实例]用户控件的设计与使用
- ASP.NET下使用WScript.Shell执行命令
- Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
- 使用VS2010代码分析功能增强ASP.NET应“.NET研究”用程序安全
- asp.net微软图表控件使用示例代码分享
- ASP.NET中使用代码来进行备份和还原数据库