Link to home
Start Free TrialLog in
Avatar of cholden
cholden

asked on

One App Running at a time

I have a C# app that should not have multiple instances running.

And if there is another instance running then kill the old process and run the one user is currently opening.

Thanks
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

Maybe the simplest way:
using System.Diagnostics;
Process[] processes = Process.GetProcessesByName("myapp");
foreach (Process p in processes)
{
    IntPtr pFoundWindow = p.MainWindowHandle;
    // Do something with the handle...
    p.CloseMainWindow();
}
http://msdn.microsoft.com/en-us/library/system.diagnostics.process.getprocessesbyname.aspx
ASKER CERTIFIED SOLUTION
Avatar of Chuck Yetter
Chuck Yetter
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ooops... forgot to say, put that code into Program.cs.
Avatar of cholden
cholden

ASKER

Axshun I used your code and it seems to work except for my Form1 is hiden and when I run the code it becomes visible.

How can I solve this?

Thanks
Avatar of cholden

ASKER

Got it thanks Axshun.