Dear All,
I m working with an C# application. My application is working properly but if I open another instance of exe (by clicking shortcut on desktop icon) It creates problem.
I want to know how to prevent another instance of exe file or how to check the status of exe file in C#?
Thank you,
With Regards
Niraj Shah
New to Smart Device | | Niraj.Shah Tuesday, October 06, 2009 3:44 PM | Google for:
"c# check if program already running"
There are lots of hits.
This seems to be a popular code path:
bool IsApplicationAlreadyRunning()
{
string proc=Process.GetCurrentProcess().ProcessName;
Process[] processes=Process.GetProcessesByName(proc);
if (processes.Length > 1)
return true;
else
return false;
}
- Marked As Answer byHarry ZhuMSFT, ModeratorTuesday, October 13, 2009 1:59 AM
- Proposed As Answer byHarry ZhuMSFT, ModeratorMonday, October 12, 2009 3:15 AM
-
| | jgalley Tuesday, October 06, 2009 4:17 PM | A very good option for this scenario is to use a Mutex . You can use a mutex to prevent a second copy of your application from running. Joel Bennett posted some sample code on his blog . The nice thing about using a mutex is that it's:
- Reliable
- Easy to notify the user - you can allow your application to run enough to tell the user "this is already running", and even activate the other copy, if you so choose.
- Easy to implement
Reed Copsey, Jr. - http://reedcopsey.com- Marked As Answer byHarry ZhuMSFT, ModeratorTuesday, October 13, 2009 1:59 AM
- Proposed As Answer byHarry ZhuMSFT, ModeratorMonday, October 12, 2009 3:15 AM
-
| | Reed Copsey, Jr. Tuesday, October 06, 2009 4:23 PM | A quick search turned this up. Untested.
Be sure to add a reference to Microsoft.VisualBasic.dll, and a using statement.
/******************************************/
static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); SingleInstanceApplication.Run(new Form1(), StartupNextInstanceHandler); }
static void StartupNextInstanceHandler( object sender, StartupNextInstanceEventArgs e) { // do whatever you want here with e.CommandLine... } }
/*******************************************/
public class SingleInstanceApplication : WindowsFormsApplicationBase { private SingleInstanceApplication() { base.IsSingleInstance = true; } public static void Run(Form f, StartupNextInstanceEventHandler startupHandler) { SingleInstanceApplication app = new SingleInstanceApplication(); app.MainForm = f; app.StartupNextInstance += startupHandler; app.Run(Environment.GetCommandLineArgs()); } }
/********************************************/
Mark the best replies as answers. "Fooling computers since 1971." - Marked As Answer byHarry ZhuMSFT, ModeratorTuesday, October 13, 2009 1:59 AM
- Proposed As Answer byHarry ZhuMSFT, ModeratorMonday, October 12, 2009 3:15 AM
-
| | Rudedog2 Tuesday, October 06, 2009 4:25 PM | Google for:
"c# check if program already running"
There are lots of hits.
This seems to be a popular code path:
bool IsApplicationAlreadyRunning()
{
string proc=Process.GetCurrentProcess().ProcessName;
Process[] processes=Process.GetProcessesByName(proc);
if (processes.Length > 1)
return true;
else
return false;
}
- Marked As Answer byHarry ZhuMSFT, ModeratorTuesday, October 13, 2009 1:59 AM
- Proposed As Answer byHarry ZhuMSFT, ModeratorMonday, October 12, 2009 3:15 AM
-
| | jgalley Tuesday, October 06, 2009 4:17 PM | A very good option for this scenario is to use a Mutex . You can use a mutex to prevent a second copy of your application from running. Joel Bennett posted some sample code on his blog . The nice thing about using a mutex is that it's:
- Reliable
- Easy to notify the user - you can allow your application to run enough to tell the user "this is already running", and even activate the other copy, if you so choose.
- Easy to implement
Reed Copsey, Jr. - http://reedcopsey.com- Marked As Answer byHarry ZhuMSFT, ModeratorTuesday, October 13, 2009 1:59 AM
- Proposed As Answer byHarry ZhuMSFT, ModeratorMonday, October 12, 2009 3:15 AM
-
| | Reed Copsey, Jr. Tuesday, October 06, 2009 4:23 PM | A quick search turned this up. Untested.
Be sure to add a reference to Microsoft.VisualBasic.dll, and a using statement.
/******************************************/
static class Program { [STAThread] static void Main() { Application.EnableVisualStyles(); SingleInstanceApplication.Run(new Form1(), StartupNextInstanceHandler); }
static void StartupNextInstanceHandler( object sender, StartupNextInstanceEventArgs e) { // do whatever you want here with e.CommandLine... } }
/*******************************************/
public class SingleInstanceApplication : WindowsFormsApplicationBase { private SingleInstanceApplication() { base.IsSingleInstance = true; } public static void Run(Form f, StartupNextInstanceEventHandler startupHandler) { SingleInstanceApplication app = new SingleInstanceApplication(); app.MainForm = f; app.StartupNextInstance += startupHandler; app.Run(Environment.GetCommandLineArgs()); } }
/********************************************/
Mark the best replies as answers. "Fooling computers since 1971." - Marked As Answer byHarry ZhuMSFT, ModeratorTuesday, October 13, 2009 1:59 AM
- Proposed As Answer byHarry ZhuMSFT, ModeratorMonday, October 12, 2009 3:15 AM
-
| | Rudedog2 Tuesday, October 06, 2009 4:25 PM |
|