首页  ·  知识 ·  
Label
      编辑:  图片来源:网络
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/
CIO之家 www.ciozj.com 微信公众号:imciow
   
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读
也许感兴趣的
我们推荐的
主题最新
看看其它的
收藏至微信