views:

2556

answers:

9

I have C# winforms application that needs to start an external exe from time to time, but I do not wish to start another process if one is already running, but rather switch to it.

So how in C# would I so this in the example below?

using System.Diagnostics;

...

Process foo = new Process();

foo.StartInfo.FileName = @"C:\bar\foo.exe";
foo.StartInfo.Arguments = "Username Password";

bool isRunning = //TODO: Check to see if process foo.exe is already running


if (isRunning)
{
   //TODO: Switch to foo.exe process
}
else
{
   foo.Start(); 
}
+11  A: 

This should do it for ya.

Check Processes

//Namespaces we need to use
using System.Diagnostics;

public bool IsProcessOpen(string name)
{
    //here we're going to get a list of all running processes on
    //the computer
    foreach (Process clsProcess in Process.GetProcesses) {
     //now we're going to see if any of the running processes
     //match the currently running processes. Be sure to not
     //add the .exe to the name you provide, i.e: NOTEPAD,
     //not NOTEPAD.EXE or false is always returned even if
     //notepad is running.
     //Remember, if you have the process running more than once, 
     //say IE open 4 times the loop thr way it is now will close all 4,
     //if you want it to just close the first one it finds
     //then add a return; after the Kill
     if (clsProcess.ProcessName.Contains(name))
     {
      //if the process is found to be running then we
      //return a true
      return true;
     }
    }
    //otherwise we return a false
    return false;
}


DaveK
Would it not be save to use Equals instead of contains, your comment does not reflect how thats going to behave.<br>If i spesifc name to be "notepad" its going to find notepad.exe and any other proccess that has notepad in its name.
EKS
+3  A: 

You can simply enumerate processes using Process.GetProcesses method.

aku
+1  A: 

I think the complete answer to your problem requires understanding of what happens when your application determines that an instance of foo.exe is already running i.e what does '//TODO: Switch to foo.exe process' actually mean?

jmatthias
+3  A: 

I have used the AppActivate function in VB runtime to activate an existing process. You will have to import Microsoft.VisualBasic dll into the C# project.

using System;
using System.Diagnostics;
using Microsoft.VisualBasic;

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            Process[] proc = Process.GetProcessesByName("notepad");
            Interaction.AppActivate(proc[0].MainWindowTitle);
        }
    }
}
Gulzar
+6  A: 

You can use LINQ as well,

var processExists = Process.GetProcesses().Any(p => p.ProcessName.Contains("<your process name>"));
aku
+1  A: 

In a past project I needed to prevent multiple execution of a process, so I added a some code in the init section of that process which creates a named mutex. This mutext was created and acquired before continuing the rest of the process. If the process can create the mutex and acquire it, then it is the first one running. If another process already controls the mutex, then the one which fails is not the first so it exits immediately.

I was just trying to prevent a second instance from running, due to dependencies on specific hardware interfaces. Depending on what you need with that "switch to" line, you might need a more specific solution such as a process id or handle.

Also, I had source code access to the process I was trying to start. If you can not modify the code, adding the mutex is obviously not an option.

Mnebuerquo
A: 

Mnebuerquo wrote:

Also, I had source code access to the process I was trying to start. If you can not modify the code, adding the mutex is obviously not an option.

I don't have source code access to the process I want to run.

I have ended up using the proccess MainWindowHandle to switch to the process once I have found it is alread running:

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 public static extern bool SetForegroundWindow(IntPtr hWnd);
csjohnst
+1  A: 

Two concerns to keep in mind:

  1. Your example involved placing a password on a command line. That cleartext representation of a secret could be a security vulnerability.

  2. When enumerating processes, ask yourself which processes you really want to enumerate. All users, or just the current user? What if the current user is logged in twice (two desktops)?

Jay Bazuzi
A: 

Good Work regarding the queries posted here !!

Ram Lal