By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,939 Members | 1,541 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,939 IT Pros & Developers. It's quick & easy.

Check On External Process

P: n/a
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

Nov 9 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
On Nov 9, 6:47 am, ags5406 <my.spam.5...@gmail.comwrote:
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

Actually, a the system.diagnotics.process class does have an Exited
event that you can hook into. You need to obviously keep a reference
to the process, and you need to set the EnableRaisingEvents property
to true. Then you have to add a handler to the Exited event.

--
Tom Shelton

Nov 9 '07 #2

P: n/a
Tom -

Thanks I was able to to use your method with success.

Nov 9 '07 #3

P: n/a
Okay I may just be creating one long conversation with myself here but
this may help someone else in the future.

I've narrowed the problem down to the "current working directory."
When I launch sample.exe manually from the folder where it is
location, the CWD gets set to that folder. So then the FORTRAN dll's
use that CWD (i'm not sure how they get it) to look for the config
files.

But when the service launches sample.exe, even though it's located in
folder C:\full path\etc\etc..., the CWD gets set to C:\Windows
\System32, and the fortran dll looks there for the config files (and
bombs because they aren't there).

So I'm now trying to figure out how to set the CWD to the location of
the sample.exe when the service launches it. I'm guessing I do this
from within the code for the service.
Nov 12 '07 #4

P: n/a
Okay I may just be creating one long conversation with myself here but
this may help someone else in the future.

I've narrowed the problem down to the "current working directory."
When I launch sample.exe manually from the folder where it is
location, the CWD gets set to that folder. So then the FORTRAN dll's
use that CWD (i'm not sure how they get it) to look for the config
files.

But when the service launches sample.exe, even though it's located in
folder C:\full path\etc\etc..., the CWD gets set to C:\Windows
\System32, and the fortran dll looks there for the config files (and
bombs because they aren't there).

So I'm now trying to figure out how to set the CWD to the location of
the sample.exe when the service launches it. I'm guessing I do this
from within the code for the service.
Nov 12 '07 #5

P: n/a
Solved it finally.

Public Sub ServerLaunch()

Environment.CurrentDirectory = "C:\full path\"

Dim bServerRunning As Boolean = False
Dim procDummy As Process
Dim procList() As Process
procList = Process.GetProcesses

For Each procDummy In procList
If procDummy.ProcessName.ToUpper = "SAMPLE" Then
bServerRunning = True
Exit For
End If
Next procDummy

procServer.EnableRaisingEvents = True
If Not bServerRunning Then
If File.Exists("sample.exe") Then
procServer.StartInfo.FileName = "sample.exe"
procServer.StartInfo.UseShellExecute = True
procServer.Start()
End If
End If

End Sub
Nov 12 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.