Windows 服務,不能啟動的淺層推測

來源:互聯網
上載者:User
大家可能都遇到過windows某個服務無法操作的情況,無法停止或者無法啟動的現象。原來對Windows服務的本質不太瞭解,今天我想我可能切中要害了。先看下文,再聽我給您講。希望大家指出不對之處。Multithreaded Services

The service control manager (SCM) controls a service by sending service control events to the service's control handler routine. The service must respond to control events in a timely manner so that the SCM can keep track of the state of the service. Also, the state of the service must match the description of its state that the SCM receives.

Due to this communication mechanism between a service and the SCM, you must be careful when using multiple threads in a service. When a service is instructed to stop by the SCM, it must wait for all the threads to exit before reporting to the SCM that the service is stopped. Otherwise, the SCM can become confused about the state of the service and might fail to shut down correctly.

The SCM needs to be notified that the service is responding to the stop control event and that progress is being made in stopping the service. The SCM will assume the service is making progress if the service responds (through SetServiceStatus) within the time (wait hint) specified in the previous call to SetServiceStatus, and the check point is updated to be greater than the checkpoint specified in the previous call to SetServiceStatus.

If the service reports to the SCM that the service has stopped before all threads have exited, it is possible that the SCM will interpret this as a contradiction. This might result in a state where the service cannot be stopped or restarted.

 

原來可能是多線程服務的代碼設計的不周全,導致上文中紅色部分描述的情況出現了。

那也就說明了,為什麼有的時候重啟之後就好了(作業系統重啟,原來未退出的進程自然早沒了,問題自然也就沒了。)

 

因為,作為程式員,我想我也找到了相關的解決辦法,用工具(Process Monitor或者ProcessExplorer)殺死線程,不過這可不是件容易乾的活,對一般人來說,還是重啟簡單穩當點。

 

 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.