CanStop 設置為 False 時停止 Windows 服務的方法 (C#) (Way to Stop a Windows Service when CanStop is set to False (C#))


問題描述

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 ToolWalden LeverichTimothy Carter)

參考文件

  1. Way to Stop a Windows Service when CanStop is set to False (C#) (CC BY‑SA 2.5/3.0/4.0)

#service #C# #Windows






相關問題

服務間數據的參照完整性 (Referential Integrity of Data Between Services)

系統重新啟動時自動啟動星號 (start asterisk autometically when system got restarted)

CanStop 設置為 False 時停止 Windows 服務的方法 (C#) (Way to Stop a Windows Service when CanStop is set to False (C#))

Android: App Widget trong quá trình cập nhật hiển thị hình ảnh lạ (Android: App Widget during update shows strange images)

查找具有特定 startname 的特定服務 (Finding specific services with specific startname)

如何判斷方法是否從 .Net(託管)代碼中的 Windows 服務調用 (How to tell if method is called from Windows Service in .Net (managed) code)

如何使用主屏幕小部件按鈕關閉服務? (How to use a homescreen widget button to shutdown a service?)

如何將版本號放入 rdlc 文件中? (How do you put a version number into an rdlc file?)

android asynTask 到活動問題? (android asynTask to activity problem ?)

GetCurrentDirectory 並沒有真正返回可執行文件的路徑 (GetCurrentDirectory does not really return the path of the executable file)

通知未顯示,因為服務或警報管理器已被終止 (Notification not showing because service or alarm manager is killed off)

當 gunicorn / celery 服務重新啟動時,Django 中有沒有辦法只執行一次 python 代碼? (Is there a way in Django to execute some python code only once when gunicorn / celery services are getting restarted?)







留言討論