prevent multiple instances of an application

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

How can I prevent multiple instances of a C# windows forms application.
VB.NET provides My.Application.StartupNextInstance Event to do this. Is there
any C# equivalent as well?

Thanks
 
Process.GetProcessByName(); search for the name of your application in the
currently running processes. If it's there, you are already running, then
exit current instance of the app.
 
Lou said:
Process.GetProcessByName(); search for the name of your application in the
currently running processes. If it's there, you are already running, then
exit current instance of the app.

Well this COULD work. Since you can name your application as you like
for example notepad
you will get some strange behavior. A better solution is to use a
mutex. A mutex is accessable from different applications and the ctor
of a mutex provides an out parameter that tells you whether the mutex
was newly created (then you should start up your application) or not.
 
Back
Top