A
ags5406
I've a Windows Service that keeps a particular executable running. If
the executable fails for whatever reason, the Service restarts it.
Right now I'm using a loop to check if the process is running. I
started with an infinite loop that ate up all of my resources, but
then added a 2 second pause after every check, which seems to have
mostly eliminated the problem of my resources being used up.
However, I'm wondering if there is a better way? Does the service
still "waste" resources while waiting for that two seconds? Is there
some kind of "process ended" event that can trigger the code to
restart the process? Following is pretty much my entire code for the
service. Pretty basic.
Imports System.Threading
Public Class fxpsMain
Friend oStartThread As Thread
Friend oStopThread As Thread
Protected Overrides Sub OnStart(ByVal args() As String)
Dim oRun As ServerTask
oRun = New ServerTask
oStartThread = New Thread(New ThreadStart(AddressOf
oRun.ContinuousRun))
oStartThread.Start()
End Sub
Protected Overrides Sub OnStop()
oStartThread.Abort()
Dim oRun As ServerTask
oRun = New ServerTask
oStopThread = New Thread(New ThreadStart(AddressOf oRun.Kill))
oStopThread.Start()
End Sub
End Class
Public Class ServerTask
Public Sub Launch()
Dim bServerRunning As Boolean = False
Dim procA As Process
Dim processList() As Process
processList = Process.GetProcesses
For Each procA In processList
If procA.ProcessName.ToUpper = "PROCESSNAME" Then
bServerRunning = True
End If
Next
If Not bServerRunning Then
Dim oProc As New Process
oProc.StartInfo.FileName = "c:\somedir\processname.exe"
oProc.StartInfo.UseShellExecute = True
oProc.Start()
End If
End Sub
Public Sub ContinuousRun()
LaunchAgain:
Launch()
'pause for 2 seconds before checking again to conserve
resources
System.Threading.Thread.Sleep(2000)
GoTo LaunchAgain
End Sub
Public Sub Kill()
Dim procA As Process
Dim processList() As Process
processList = Process.GetProcesses
For Each procA In processList
If procA.ProcessName.ToUpper = "c:\somedir
\processname.exe" Then
procA.Kill()
End If
Next
End Sub
End Class
the executable fails for whatever reason, the Service restarts it.
Right now I'm using a loop to check if the process is running. I
started with an infinite loop that ate up all of my resources, but
then added a 2 second pause after every check, which seems to have
mostly eliminated the problem of my resources being used up.
However, I'm wondering if there is a better way? Does the service
still "waste" resources while waiting for that two seconds? Is there
some kind of "process ended" event that can trigger the code to
restart the process? Following is pretty much my entire code for the
service. Pretty basic.
Imports System.Threading
Public Class fxpsMain
Friend oStartThread As Thread
Friend oStopThread As Thread
Protected Overrides Sub OnStart(ByVal args() As String)
Dim oRun As ServerTask
oRun = New ServerTask
oStartThread = New Thread(New ThreadStart(AddressOf
oRun.ContinuousRun))
oStartThread.Start()
End Sub
Protected Overrides Sub OnStop()
oStartThread.Abort()
Dim oRun As ServerTask
oRun = New ServerTask
oStopThread = New Thread(New ThreadStart(AddressOf oRun.Kill))
oStopThread.Start()
End Sub
End Class
Public Class ServerTask
Public Sub Launch()
Dim bServerRunning As Boolean = False
Dim procA As Process
Dim processList() As Process
processList = Process.GetProcesses
For Each procA In processList
If procA.ProcessName.ToUpper = "PROCESSNAME" Then
bServerRunning = True
End If
Next
If Not bServerRunning Then
Dim oProc As New Process
oProc.StartInfo.FileName = "c:\somedir\processname.exe"
oProc.StartInfo.UseShellExecute = True
oProc.Start()
End If
End Sub
Public Sub ContinuousRun()
LaunchAgain:
Launch()
'pause for 2 seconds before checking again to conserve
resources
System.Threading.Thread.Sleep(2000)
GoTo LaunchAgain
End Sub
Public Sub Kill()
Dim procA As Process
Dim processList() As Process
processList = Process.GetProcesses
For Each procA In processList
If procA.ProcessName.ToUpper = "c:\somedir
\processname.exe" Then
procA.Kill()
End If
Next
End Sub
End Class