問題描述
CanStop 設置為 False 時停止 Windows 服務的方法 (C#) (Way to Stop a Windows Service when CanStop is set to False (C#))
好的,我沒有意願的第二部分電源實驗 是:
總結性問題 ‑ 有沒有辦法在 Windows 服務上動態設置 CanStop 屬性?
Whole Spiel ‑我有一項服務,目前正在檢查和終止進程(IE 遊戲),我已經告訴它,如果那天我不允許。偉大的。我將 CanStop 設置為 false,這樣如果我沉迷其中,就不能直接終止服務。我有一個程序將進行密碼檢查(其他人輸入密碼),如果密碼正確,它將停止服務。(如果我有嚴重的提款)問題是使用 ServiceController 類。
據我所知,ServiceController 只是一個裝飾器(是的設計模式猜測),所以我無法獲得它所代表的實際服務。第一次嘗試是屬性信息,但我太笨了,沒有意識到那是沒有意義的。其次是字段信息,因為我認為可能有一個“代表”服務的私有字段。正如您可能猜到的那樣,兩者都失敗了。
有什麼想法嗎?
編輯 1 我想避免將 CanStop 值放在我可以得到的地方很容易像配置文件或註冊表一樣。所以我正在嘗試,雖然沒有成功,但在程序中完全處理這個問題。
新(失敗)嘗試:
ManagementObject 服務;ManagementBaseObject 停止服務;
service = new ManagementObject("Win32_Service.Name='StopProgram'");
stopService = service .InvokeMethod("StopService", null, null);
blockquote>不確定這有什麼作用。我認為由於 CanStop 情況它無法停止。
參考解法
方法 1:
The "CanStop" is a attribute of the services registration in the windows service control manager. You can't change it mid‑stride.
And, of course, if you're smart enough to write your own service then you're smart enough to bring up task‑man and simply kill the service process. CanStop will not prevent you from pulling the rug out from under the service. CanStop only tells the service control manager not to send "Stop" commands to the service.
If you want to allow something to pass then use a global event to enable/disable the checking the service does ‑‑ or just remove the games from the PC! :‑)
方法 2:
Rather than trying to directly access and control the Service, could you set a flag somewhere, (like the registry or a file), that is then checked by your service before it executes the Event you're trying to control.
(by Programmin Tool、Walden Leverich、Timothy Carter)
參考文件