首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > .NET >

windows服务有关问题(超)

2012-10-23 
windows服务问题(超高手进)private bool isrunprotected override void OnStart(string[] args){isrun t

windows服务问题(超高手进)
private bool isrun;

protected override void OnStart(string[] args)
  {

  isrun =true;
  while (isrun)
  {
  try
  {
  StreamWriter sw = File.AppendText(@"d:\myserver.txt");
  sw.WriteLine(string.Format("服务启动,启动时间{0}", DateTime.Now));
  }
  }
  catch (Exception ex)
  {
  WirteWinLog(ex);
  }

  System.Threading.Thread.Sleep(10*1000);
  }

  }

  protected override void OnStop()
  {

  isrun=false;
  StreamWriter sw = File.AppendText(@"d:\myserver.txt");
  sw.WriteLine(string.Format("服务停止,启动时间{0}", DateTime.Now));
  sw.Close();
  }

为什么服务发布后无法停止呢????



[解决办法]
protected override void OnStart(string[] args)
{

System.Timers.Timer aTimer = new System.Timers.Timer();

// Hook up the Elapsed event for the timer.
aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

// Set the Interval to 2 seconds (2000 milliseconds).
aTimer.Interval = 20000;
aTimer.Enabled = true;

// Keep the timer alive until the end of Main.
GC.KeepAlive(aTimer);
//DataReceive.Listener();

}

protected override void OnStop()
{
timer1.Enabled = false;
}
这样就停了

热点排行