Defect program running

bungpeng

Senior Contributor
Joined
Sep 10, 2002
Messages
906
Location
Malaysia
Is there anyway I can defect whether a program is running?

For example:
1. I have 2 program "A", and "B".
2. "A" program can trigger "B" program to run. (like Shell command in VB6)
3. But if "B" is already running, then "A" cannot ask "B" to run again.

So, is there anyway I can defect whether "B" program is running?

Thank you.
 
If you're using Process.Start to start the other process, you can wait for the Exited event of the Process class returned to see when it has finished executing. You'll have to set .EnableRaisingEvents to true before it will raise the event though.
 
I don't know what you mean by "defect", I can only assume you mean to say "detect" every time.

There is a method of the process class that allows you to get a collection of all running processes on the system. Iterating through this should be able to tell you if your process is running or not.
 
Sorry! I did a mistake, what you assume is right, it is "detect", not "defect".

May I know what process class it is? that is what I looking for...
 
Here is a function that seems to work. Just pass it the name of the
process (without extention [notepad, not notepad.exe, etc.]) and
it will return a boolean value.

Visual Basic:
    Public Function IsProcessRunning(ByVal process As String) As Boolean
        Dim processes As Process(), tmp As New Process()
        processes = tmp.GetProcesses

        'Enumerate the processes and check to see if the process is there
        For Each tmp In processes
            If tmp.ProcessName = process Then Return True
        Next

        Return False
    End Function
It could probably be optimized by some use of IndexOf, but since you
can't create a Process object and then change its properties, it
wouldn't work as easily as that.
 
Back
Top