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("服務(wù)啟動(dòng),啟動(dòng)時(shí)間{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("服務(wù)停止,啟動(dòng)時(shí)間{0}", DateTime.Now)); sw.Close(); }為什么服務(wù)發(fā)布后無法停止呢????
2 回答

慕森卡
TA貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊
因?yàn)?你的service 永遠(yuǎn)不會(huì)從 onstart method里退出. 一直在while loop里 運(yùn)行, onstop 沒有機(jī)會(huì)執(zhí)行。
你可以用一個(gè) backgroundWorker, onstart 里 初始化worker, 工作在 worker 里做, onstop把worker停止。
- 2 回答
- 0 關(guān)注
- 1214 瀏覽
添加回答
舉報(bào)
0/150
提交
取消