当前位置: 代码迷 >> .NET新技术 >> windows服务有关问题(超)
  详细解决方案

windows服务有关问题(超)

热度:314   发布时间:2016-04-25 01:41:00.0
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;
}
这样就停了
  相关解决方案