467,894 Members | 1,880 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,894 developers. It's quick & easy.

Check whether an application is running

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
Nov 16 '05 #1
  • viewed: 8678
Share:
3 Replies
In article <uw**************@TK2MSFTNGP09.phx.gbl>, "pnp" <pnp.at.softlab.ece.ntua.gr>
says...
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.");
}
=====================================
Nov 16 '05 #2
pnp
thanks John.

"John Nelson" <ne**@scapeware.com> wrote in message
news:MP************************@news.comcast.gigan ews.com...
In article <uw**************@TK2MSFTNGP09.phx.gbl>, "pnp" <pnp.at.softlab.ece.ntua.gr> says...
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.");
}
=====================================

Nov 16 '05 #3
"John Nelson" <ne**@scapeware.com> wrote in message
news:MP************************@news.comcast.gigan ews.com...
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...
Nov 16 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

12 posts views Thread by tshad | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.