public void Initaa()
{
// 新建一个TimerCallback委托,具体要执行的方法在ScheduledWorkCallbackEmailInterval中
emailTimer = new Timer(new TimerCallback(ScheduledWorkCallbackEmailInterval), null, 5000, 2000);
}
/**////
/// 释放定时器
/// public void displose()
{
statsTimer = null;
emailTimer = null;
}
/**////
/// 定时任务
/// private void ScheduledWorkCallbackEmailInterval (object sender)
{
try
{
StreamWriter sw;
string Path = Server.MapPath(".");
int last = Server.MapPath(".").LastIndexOf("\\");
Path = Path.Substring(0,last)+"\\index.txt";
if(!File.Exists(Path))
{
FileStream fs = File.Create(Path);
sw = new StreamWriter(Path,false,System.Text.Encoding.GetEncoding("gb2312"));
}
else
{
sw = new StreamWriter(Path,true,System.Text.Encoding.GetEncoding("gb2312"));
}
sw.WriteLine("!");
sw.WriteLine("!");
sw.WriteLine("!");
sw.WriteLine("------------"+DateTime.Now.ToString()+"----------------");
sw.Close();
}
catch
{
Response.Write(2);
}
finally
{
emailTimer.Change( 5000, 5000 );
}
}
////
/// 定时休眠
/// private void ScheduledWorkCallbackStatsInterval(object sender)
{
try
{
// 休眠定时器
statsTimer.Change( System.Threading.Timeout.Infinite, 2000 );
}
catch( Exception e )
{
}
finally
{
// 唤醒定时器
statsTimer.Change( 5000, 2000);
}
}
最后调用Initaa() 这个函数就可以了。作用是给文本文件中每个5秒写一次数据。
本文作者:ashou706 来源:http://www.cnblogs.com/ashou706/