How to get a .Net service to restart itself
I had a few problems recently attempting to trigger a .Net service to restart itself especially if changes were made to its configuration file. For a while I was hung up on the restart until I found a sample batch file command to pause in between stop and start calls at http://www.robvanderwoude.com/wait.php.
Here's the sampleVB.Net code:
Imports System.Runtime.CompilerServices
Public Module ServiceControllerExtension
<Extension()> _
Public Sub Restart(s As System.ServiceProcess.ServiceController)
Dim tempBatch As String = System.IO.Path.GetTempFileName()
System.IO.File.Move(tempBatch, tempBatch & ".bat")
tempBatch &= ".bat"
Using w As New System.IO.StreamWriter(tempBatch)
w.WriteLine("net stop """ & s.ServiceName & """")
w.WriteLine("REM The following line simply pauses for 3 seconds")
w.WriteLine("CHOICE /C:AB /T:3 /D:A > NUL") 'This line just pauses.
'Somehow a lack of a pause prevents the service from starting up.
'w.WriteLine("ping localhost") 'alternate to above line
w.WriteLine("net start """ & s.ServiceName & """")
w.WriteLine("del /Q """ & tempBatch & """")
End Using
Process.Start(tempBatch)
End Sub
End Module
Here is a sample of how to use it:
Private Sub SomeMethod()
Dim x As New ServiceProcess.ServiceController("MyServiceName")
x.Restart()
End Sub