prevent multiple instances of an application

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
 
L

Lou Jackson

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.
 
R

Roman Wagner

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top