關於朱泰銓-個人資料



[VB.NET]定時重新啟動服務 (Service Timer)

最近因為有定時重新啟動服務的需要,所以用vb.net寫了個定時重新啟動服務的程式
分享原始碼給大家
其實原理很簡單
短短的幾行程式就能完成

利用.NET現有的控制項就能做的到,看來.net開發起來真的是便利性十足

——————————————-
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Dim sc As New ServiceProcess.ServiceController()
sc.ServiceName = "服務的名稱"
If sc.Status = ServiceProcess.ServiceControllerStatus.Running Then
sc.Stop()
sc.WaitForStatus(ServiceProcess.ServiceControllerStatus.Stopped)
End If
If sc.Status = ServiceProcess.ServiceControllerStatus.Stopped Then
Try
sc.Start()
sc.WaitForStatus(ServiceProcess.ServiceControllerStatus.Running)
Catch
End Try

End If
sc.Dispose()
end sub

——————————————————

2 comments to [VB.NET]定時重新啟動服務 (Service Timer)

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>