473,394 Members | 1,841 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Check On External Process

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
5 1789
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
Tom -

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

Nov 9 '07 #3
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Juan Manuel Alegrķa B. | last post by:
Hi group I have been making a windows service to execute an external application. I use a timer control, I can execute the application but does't appear as a normal windows, just I see it in the...
2
by: SC | last post by:
Hi, I want tu run an external program from a c# program but i need to wait until the external program ends. How can i do that? I try Process.Start but it do asynchronously... Any help? Bye
4
by: My SQL | last post by:
Hi Can I trigger an external program to run when a new record is added to the mysql table? The external program is in C and instead of scanning the table continuously for new insertions, it...
0
by: lcifers | last post by:
I have written an application and tested it as a Windows executable. It works fine. The application uses an external COM application to process some existing files. But I can't seem to start that...
5
by: snicks | last post by:
I'm trying to exec a program external to my ASP.NET app using the following code. The external app is a VB.NET application. Dim sPPTOut As String sPPTOut = MDEPDirStr + sID + ".ppt" Dim p As...
2
by: holysmokes99 | last post by:
I am developing a component in .Net 1.1, and want to debug it using the "start external program" of the debugger in the IDE. The program I want to start references both 1.1 and 2.0 components. The...
1
by: =?Utf-8?B?Q3JhaWc=?= | last post by:
Hi Guys, I am trying to caputre the output for an external application. The idea is to use the System.Diagnostics.Process to run the exe in a process and redirect the output to a string. When...
1
by: webotronics | last post by:
Hi, I need to add a timeout for external programs, as the external program sometimes never dies (it's a ClearQuest multisite call to the shipping server, that sometimes never ends, but simply...
2
by: Smokey Grindel | last post by:
Is there anyway to check if an external process is running, then terminate it if it is? my problem right now is in my installer the user might try to upgrade while the program is running... I want...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.