public void Initaa()
  {  
 
   // 新建一个TimerCallback委托,具体要执行的方法在ScheduledWorkCallbackEmailInterval中 
   emailTimer = new Timer(new TimerCallback(ScheduledWorkCallbackEmailInterval), null, 5000, 2000);      
  } 
 
  /**//// <summary> 
  /// 释放定时器 
  /// </summary> 
  public void displose()
  { 
   statsTimer = null; 
   emailTimer = null; 
  } 
 
  /**//// <summary> 
  /// 定时任务
  /// </summary> 
  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 ); 
    } 
   } 
 
  //// <summary> 
  /// 定时休眠
  /// </summary> 
  private void ScheduledWorkCallbackStatsInterval(object sender)
  { 
   try
   { 
    // 休眠定时器 
    statsTimer.Change( System.Threading.Timeout.Infinite, 2000 ); 
     } 
   catch( Exception e )
   {
 
   } 
   finally
   { 
    // 唤醒定时器 
    statsTimer.Change( 5000, 2000); 
   } 
  } 

最后调用Initaa() 这个函数就可以了。作用是给文本文件中每个5秒写一次数据。

相关文章:

  • 2022-02-23
  • 2022-02-19
  • 2022-01-29
  • 2022-12-23
  • 2021-07-12
  • 2021-09-02
  • 2021-08-03
  • 2022-12-23
猜你喜欢
  • 2021-06-29
  • 2021-11-27
  • 2022-12-23
  • 2022-12-23
  • 2021-05-21
相关资源
相似解决方案