tags:

views:

34

answers:

3

I'm using the Process Class to start processes, but don't ever want more than one instance of any program to be running.

Looking at the documentation, there are lots of likely-looking properties, but nothing that stands out as the most obvious.

What's the best way to determine if a process is running?

Edit: John Fisher is right: it's an existing application that I'm starting and I'm unable to modify it.

A: 

You should use the Singleton application pattern for that:

bool createdNew = true;
using (var mutex = new Mutex(true, "YourProcessName", out createdNew))
{
    if (createdNew)
    {
        // Run application
    }
}
Shay Erlichmen
This will only work if he's trying to start an app he's written and can modify.
John Fisher
+1  A: 

I guess that all depends on what you mean by "best way"? Do you mean the fastest, the most accurate, or one that will handle some odd circumstances?

The way I would start is by listing the processes and checking the executable file name against the one I'm trying to start. If they match (case insensitive), it's probably running.

John Fisher
+1  A: 

You can call this method

Process.GetProcesses()

and loop through the result (a collection of type Process) to see if the name matches. Something like this:

foreach (Process prc in Process.GetProcesses())
{
    if (prc.ProcessName.Contains(MyProcessName))
    {
        //Process is running
    }
}
Gabriel McAdams