我的windows服務正在OnStart()方法中創建1個線程。線程的代碼包含1個while循環,看起來像這樣:如何停止Windows服務C#?
Thread mworker;
AutoResetEvent mStop = new AutoResetEvent(false);
protected override void OnStart(string[] args)
{
// TODO: Add code here to start your service.
mworker = new Thread(pwd_fetch);
mworker.IsBackground = false;
mworker.Start();
}
protected override void OnStop()
{
// TODO: Add code here to perform any tear-down necessary to stop your service.
mStop.Set();
mworker.Join();
}
private void pwd_fetch()
{
while(true)
{
//some other code
if (mStop.Set()==true)
break;
}
}
我想while循環的條件爲真,但打破我使用IF()指令循環,但仍無法停止服務。
有人知道,爲什麼這樣呢?我怎麼解決這個問題?
3.Stop服務是什麼MSTOP和設置是屬性或方法。如果是方法,則Set在每種情況下都返回false。 –
@Romil:編輯過的代碼。 – sailer
參考瞭解有關Set和WaitOne與AutoresetEvent http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx –