|
-
Apr 16th, 2007, 04:07 AM
#1
Thread Starter
Fanatic Member
[RESOLVED] [2005] Check if process is running
Hi guys! At the moment I am using the following loop to check if a process is running. Does anyone know if there is a better way of doing this?
Code:
Dim isProcessRunning As Boolean = True
Do While isProcessRunning
If (Process.GetProcessesByName("MyContacts").Length = 0) Then isProcessRunning = False
Loop
PS: I have also added code that exits the loop if it has laster for longer than 15 seconds as it tends to overload otherwise!
-
Apr 16th, 2007, 05:11 AM
#2
Re: [2005] Check if process is running
If you add an event handler to the Exited event, you will know when the process isnt alive anymore. That would be the absolute best solution since it doesnt affect your applications performance like your "busy waiting" loop does.
But if you really want to do it that way, here is how I would do it:
VB Code:
Dim p() As Process = Process.GetProcessesByName("MyContacts")
'Since p() now contains an array of processes (because there might be more than one MyContacts process on the system) I will just check if the first instance in the loop:
Do While Not p(0).HasExited
Loop
-
Apr 16th, 2007, 05:17 AM
#3
Thread Starter
Fanatic Member
Re: [2005] Check if process is running
Thanks for the reply, Atheist! The problem is that the process I am trying to find out about has not been started by my application so I could not handle an Exited event. I will try your suggestion with the HasExited property though. The program that is doing this task does not need to do anything else apart from start a process when a running existing process has finished.
-
Apr 16th, 2007, 05:22 AM
#4
Re: [2005] Check if process is running
Ah ok. Are you sure you cant handle the exited event of the process? Thats strange, does it throw any exceptions? I just tried opening two instances of notepad and then ran this code:
VB Code:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For Each p As Process In Process.GetProcessesByName("notepad")
p.EnableRaisingEvents = True
AddHandler p.Exited, AddressOf ProcessExited
Next
End Sub
Private Sub ProcessExited(ByVal sender As Object, ByVal e As System.EventArgs)
Dim exProcess As Process = DirectCast(sender, Process)
MessageBox.Show(exProcess.Id.ToString & ": " & exProcess.ProcessName & " has exited")
End Sub
It worked alright for me.
-
Apr 16th, 2007, 05:29 AM
#5
Thread Starter
Fanatic Member
Re: [2005] Check if process is running
Ah! I didn't know you could add the event to an already running process. It makes sense I suppose. Thanks, I will try that!
-
Apr 16th, 2007, 05:43 AM
#6
Thread Starter
Fanatic Member
Re: [2005] Check if process is running
I do have another question.. the program that is carrying out this function does not have any forms and is just a Sub Main method. If I add the Exited event to the process in the Sub Main method, won't the program exit as soon as the Sub Main method is finished?
-
Apr 23rd, 2007, 04:12 AM
#7
Thread Starter
Fanatic Member
Re: [2005] Check if process is running
Thanks guys! Was able to figure this one out eventually.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|