這個問題是基於其他問題: How can a windows service programmatically restart itself?問題與Windows服務程序重新啓動本身
我實現的解決方案之一在前面的問題,像這樣:
Dim proc As New Process()
Dim psi As New ProcessStartInfo()
psi.CreateNoWindow = True
psi.FileName = "cmd.exe"
psi.Arguments = "/C net stop YOURSERVICENAMEHERE && net start YOURSERVICENAMEHERE"
psi.LoadUserProfile = False
psi.UseShellExecute = False
psi.WindowStyle = ProcessWindowStyle.Hidden
proc.StartInfo = psi
proc.Start()
我看到2個問題與此:
- 事件日誌每次服務重新啓動時引發此警告:
Windows detected your registry file is still in use by other applications or services... 5 user registry handles leaked from ...
- 服務有時可能無法重新啓動。在仔細研究之後,我最好的猜測就是該服務在該進程嘗試重新啓動之前結束。這是一個問題的原因是因爲淨停止命令將失敗,因爲服務已經停止,導致它不執行net start命令。
下面是完整的代碼:
Private Sub Timer_Elapsed(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs) Handles _timer.Elapsed
' we do not want the timer stepping on itself (ie. the time interval elapses before the first call is done processing
_timer.Stop()
' do some processing here
Dim shouldRestart As Boolean = ProcessStuff()
If shouldRestart Then
Dim proc As New Process()
Dim psi As New ProcessStartInfo()
psi.CreateNoWindow = True
psi.FileName = "cmd.exe"
psi.Arguments = "/C net stop ""My Cool Service"" && net start ""My Cool Service"""
psi.LoadUserProfile = False
psi.UseShellExecute = False
psi.WindowStyle = ProcessWindowStyle.Hidden
proc.StartInfo = psi
proc.Start()
Return
End If
_timer.Start()
End Sub
假設我是正確的(重要的假設)有問題2,你認爲用了Thread.Sleep(10000)命令更換工作Return語句?
+1對於Onstop的信息。有Windows重新啓動它是一個好的選擇(見其他評論)。我將不得不使用批處理文件進行測試。該服務可以在任何用戶帳戶(不僅是本地系統)下運行,在這種情況下,該用戶可能沒有權限執行批處理文件。不確定,但我會測試。 – 2012-02-10 21:20:05
我剛發現不使用批處理文件的另一個原因。安全!如果服務作爲具有高權限的用戶運行,我想不出一個更簡單的方法來破解它。 – 2012-02-16 16:27:30
如果安全性問題,該服務可以發出自己的批處理文件。這在很大程度上排除了攻擊者劫持進程以運行惡意批處理文件的能力,並且不會比任何基於運行cmd.exe和Process.Start()的東西更加依賴「魔術字符串」如果命令需要更改,則必須重新構建應用程序的代價。 – KeithS 2012-02-16 16:34:17