Question : vb.net service needs to restart itself

Yes I know it's not easily possible, but I am looking for a way to restart a service "internally" meaning the service initiates a service restart upon itself.  It seems calls to "restart" a service to the windows api are simply "stops" then "starts".  Any ideas? I don't really want an external program as that seems a bit of a hack.

Answer : vb.net service needs to restart itself

You can use System.Diagnostics.Process to spawn a child process that uses cmd.exe to execute a command that'll stop & start your service.  The simple service below waits for a .txt file to be created in C:\, makes a copy of itself, and runs cmd.exe to stop/start the service.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
Imports System.IO
Imports System.Windows.Forms
Imports System.Threading
Imports System.Diagnostics
Public Class TestVBService
	Private fsw As FileSystemWatcher
 
	Protected Overrides Sub OnStart(ByVal args() As String)
		' Add code here to start your service. This method should set things
		' in motion so your service can do its work.
 
		fsw = New FileSystemWatcher("C:\", "*.txt")
		fsw.IncludeSubdirectories = False
		fsw.NotifyFilter = NotifyFilters.FileName
		AddHandler fsw.Created, AddressOf fiwatch_Created
		fsw.EnableRaisingEvents = True
	End Sub
 
	Protected Overrides Sub OnStop()
		' Add code here to perform any tear-down necessary to stop your service.
 
	End Sub
 
 
	Private Sub fiwatch_Created(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
		EventLog.WriteEntry("TestVBService", "Saw a file created.")
 
		Dim proc As New Process()
		Dim psi As New ProcessStartInfo()
 
		psi.CreateNoWindow = True
		psi.FileName = "cmd.exe"
		psi.Arguments = "/C net stop TestVBService && net start TestVBService"
		psi.LoadUserProfile = False
		psi.UseShellExecute = False
		psi.WindowStyle = ProcessWindowStyle.Hidden
 
		proc.StartInfo = psi
 
		proc.Start()
	End Sub
End Class
Random Solutions  
 
programming4us programming4us