473,602 Members | 2,751 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Threadin g

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(Add ressOf
oRun.Continuous Run))
oStartThread.St art()
End Sub

Protected Overrides Sub OnStop()
oStartThread.Ab ort()

Dim oRun As ServerTask
oRun = New ServerTask
oStopThread = New Thread(New ThreadStart(Add ressOf oRun.Kill))
oStopThread.Sta rt()
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.GetProc esses

For Each procA In processList
If procA.ProcessNa me.ToUpper = "PROCESSNAM E" Then
bServerRunning = True
End If
Next

If Not bServerRunning Then
Dim oProc As New Process
oProc.StartInfo .FileName = "c:\somedir\pro cessname.exe"
oProc.StartInfo .UseShellExecut e = True
oProc.Start()
End If

End Sub

Public Sub ContinuousRun()

LaunchAgain:
Launch()
'pause for 2 seconds before checking again to conserve
resources
System.Threadin g.Thread.Sleep( 2000)
GoTo LaunchAgain

End Sub

Public Sub Kill()

Dim procA As Process
Dim processList() As Process
processList = Process.GetProc esses

For Each procA In processList
If procA.ProcessNa me.ToUpper = "c:\somedir
\processname.ex e" Then
procA.Kill()
End If
Next

End Sub

End Class

Nov 9 '07 #1
5 1806
On Nov 9, 6:47 am, ags5406 <my.spam.5...@g mail.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.Threadin g

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(Add ressOf
oRun.Continuous Run))
oStartThread.St art()
End Sub

Protected Overrides Sub OnStop()
oStartThread.Ab ort()

Dim oRun As ServerTask
oRun = New ServerTask
oStopThread = New Thread(New ThreadStart(Add ressOf oRun.Kill))
oStopThread.Sta rt()
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.GetProc esses

For Each procA In processList
If procA.ProcessNa me.ToUpper = "PROCESSNAM E" Then
bServerRunning = True
End If
Next

If Not bServerRunning Then
Dim oProc As New Process
oProc.StartInfo .FileName = "c:\somedir\pro cessname.exe"
oProc.StartInfo .UseShellExecut e = True
oProc.Start()
End If

End Sub

Public Sub ContinuousRun()

LaunchAgain:
Launch()
'pause for 2 seconds before checking again to conserve
resources
System.Threadin g.Thread.Sleep( 2000)
GoTo LaunchAgain

End Sub

Public Sub Kill()

Dim procA As Process
Dim processList() As Process
processList = Process.GetProc esses

For Each procA In processList
If procA.ProcessNa me.ToUpper = "c:\somedir
\processname.ex e" Then
procA.Kill()
End If
Next

End Sub

End Class

Actually, a the system.diagnoti cs.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 EnableRaisingEv ents 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.Cur rentDirectory = "C:\full path\"

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

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

procServer.Enab leRaisingEvents = True
If Not bServerRunning Then
If File.Exists("sa mple.exe") Then
procServer.Star tInfo.FileName = "sample.exe "
procServer.Star tInfo.UseShellE xecute = True
procServer.Star t()
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
3572
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 windows task manager as a local service. I think it must appear as a normal process. Someone knows about it or has any idea? Juan Manuel Alegria B. Jalisco, México.
2
5196
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
20131
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 will be better if an external program could be triggered. Any suggestions?
0
1344
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 external program correctly. I've found this code: ' Set process parameters Dim myProcess As New System.Diagnostics.Process myProcess.StartInfo.FileName = "notepad.exe" ' Allow the process to raise events
5
3620
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 New System.Diagnostics.Process 'p.Start(MDEPDirStr & "macrun.exe", sPPTOut) p.Start("C:\WINDOWS\SYSTEM32\CALC.EXE") 'p.Start("C:\WINDOWS\SYSTEM32\macrun.exe", sPPTOut)
2
1758
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 problem is that when I launch this from VS2003, the external program starts only for a moment and then bails out of memory with no error, and the ide returns to a stopped state awaiting my input. If I remove the 2.0 framework, at least the...
1
2309
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 I run the external command it works fine. But when i wrap it in a .net exe it fails to exit and the application does not exit. If I kill the external exe from task manager, the .net wrap completes.
1
1937
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 hangs). I have three different ways of forking the call, but none works. I send the one I believe most in... use POSIX ":sys_wait_h"; my $loop = 1; $loop = $ARGV if defined $ARGV;
2
3964
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 to check if it is running, notify them it is, then ask them if they want to terminate it or not... thanks!
0
7920
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8268
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6730
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5867
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3944
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2418
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1510
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1254
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.