4

I'm working on a my Windows Forms application. I start a calculator process on a button click event using this code:

System.Diagnostics.Process.Start("calc.exe");

If I click the button again, another calculator process is started. So how do I stop the process from running more than once?

Thanks

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
kapil
  • 131
  • 1
  • 2
  • 9
  • 1
    You can check for the running instance of calc.exe, read this > http://stackoverflow.com/questions/6392031/c-sharp-how-to-check-if-another-instance-of-the-application-is-running – yogi Jun 30 '12 at 13:13
  • There are three popular ways to do that. A mutex which yogi pointed you at, Anynchronous wait for close, which Chris Gessler alluded to. The other check for process already running would find any instance of calc.exe, would find calc run by any other prgram which might not be suitable. – Tony Hopkinson Jun 30 '12 at 13:25

4 Answers4

5

Easy... disable the button until the process dies. Of course, you could also put in some kind of logic:

if(!myProcess.HasExited)
{
  alert("You've already started the calculator app.");
}
else
{
  // start the process
}

Personally, I like the former. Just set an event handler and set:

Process.EnableRaisingEvents = true;

The event handler will be raised letting your proggy know when the calculator app has exited, so it can reenable the button.

http://msdn.microsoft.com/en-us/library/system.diagnostics.process.enableraisingevents.aspx

try
{
    myProcess.EnableRaisingEvents = true;
    myProcess.Exited += new EventHandler(myProcess_Exited);
    myProcess.Start();
    // disable button
}
catch()
{
    // enable button
}


private void myProcess_Exited(object sender, System.EventArgs e)
{
    // enable button
}
Chris Gessler
  • 22,727
  • 7
  • 57
  • 83
3

Make it Simple, Make a function to check if any process is running :

public bool IsProcessOpen(string name)
{
foreach (Process process in Process.GetProcesses())
{
if (process.ProcessName.Contains(name))
{
return true;
}
}
return false;
}

Now use this function where you want to check for the process :

if (!IsProcessOpen("calc"))
 System.Diagnostics.Process.Start("calc.exe");

Update

The process of calculator in newer version of Windows is replaced with Calculator.exe you trying to check if process exist using calc.exe will always fail you need to check for both calc and calculator. Here is screen shot Update

Code

 //If calc.exe or Calculator.exe not exist then start the calculator 
if (!IsProcessOpen("calc")|| !IsProcessOpen("Calculator"))
System.Diagnostics.Process.Start("calc.exe");
TAHA SULTAN TEMURI
  • 4,031
  • 2
  • 40
  • 66
kakarott
  • 193
  • 2
  • 9
2

You could use Process.GetProcessesByName() to discover any running processes named "calc.exe". That's however unreliable, the process name is not unambiguous. There could be an entirely different process with the same .exe name.

A sane approach is to start the process and keep track of it with the Process.Exited event. Like this:

    Process calc;

    private void button1_Click(object sender, EventArgs e) {
        if (calc != null) return;
        calc = new Process();
        calc.StartInfo = new ProcessStartInfo("calc.exe");
        calc.EnableRaisingEvents = true;
        calc.Exited += new EventHandler(calc_Exited);
        calc.SynchronizingObject = this;
        calc.Start();
        button1.Enabled = false;
    }

    void calc_Exited(object sender, EventArgs e) {
        calc.Exited -= calc_Exited;
        calc = null;
        button1.Enabled = true;
    }
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
0

Normally when you do this you want to transfer control to the first instance of the application so it can activate itself. Otherwise the person launching the application doesn't know what is going on and will keep launching it.

You can implement this by creating a Mutex when your application launches, along the lines of:

 bool mine = false;
 Mutex mutex = new Mutex(true, "Something to identify this", out mine);
 if (!mine)
   // Application is already running

If the application launches a second time, the Mutex will already exist and then you can use SendMessage to the initial application to have it activate itself.

An example of how this is done is here.

Michael
  • 8,891
  • 3
  • 29
  • 42