Check whether an application is running

  • Thread starter Thread starter pnp
  • Start date Start date
P

pnp

What's the best way to check whether an app is running so if one tries to
open it again it will not open a second instance of the application but will
give focus to the already open one maximizing it...

-Peter
 
"pnp" <pnp.at.softlab.ece.ntua.gr> said:
What's the best way to check whether an app is running so if one tries to
open it again it will not open a second instance of the application but will
give focus to the already open one maximizing it...

-Peter
Try this in your main...

=====================================

System.Threading.Mutex appMutex = new System.Threading.Mutex(true,"AppName");

if(appMutex.WaitOne(0, false))
{
try
{
Application.Run(new FormMain());
}
catch(Exception EX)
{
MessageBox.Show("Application Error!!\r\n" + EX.Message);
}

}
else
{

MessageBox.Show("The application is already loaded.");
}
=====================================
 
thanks John.

John Nelson said:
What's the best way to check whether an app is running so if one tries to
open it again it will not open a second instance of the application but will
give focus to the already open one maximizing it...

-Peter
Try this in your main...

=====================================

System.Threading.Mutex appMutex = new System.Threading.Mutex(true,"AppName");

if(appMutex.WaitOne(0, false))
{
try
{
Application.Run(new FormMain());
}
catch(Exception EX)
{
MessageBox.Show("Application Error!!\r\n" + EX.Message);
}

}
else
{

MessageBox.Show("The application is already loaded.");
}
=====================================[/QUOTE]
 
System.Threading.Mutex appMutex = new System.Threading.Mutex(true,"AppName");

if(appMutex.WaitOne(0, false))
{
try
{
Application.Run(new FormMain());
}
catch(Exception EX)
{
MessageBox.Show("Application Error!!\r\n" + EX.Message);
}

}
else
{

MessageBox.Show("The application is already loaded.");
}
=====================================

I use something similar to this pretty much in every WinForms app I write as
a replacement for the old VB "App.PrevInstance()" function if I need to
prevent more than one instance of the app running...
 
Back
Top