Visual Studio Development Bookmark and Share   
 index > Visual C# Express Edition > how to stop another instance of exe thrugh c#
 

how to stop another instance of exe thrugh c#

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;
}

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
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."
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;
}

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
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."
Rudedog2  Tuesday, October 06, 2009 4:25 PM

You can use google to search for other answers

Custom Search

More Threads

• Mysql and C# question
• tabpages help
• notepad
• Exception: "Dynamic SQL generation for the UpdateCommand is not supported against a SelectCommand that does not return any key "
• printing with WebBrowser
• UI to edit XML Configuration
• how to change diacritics color in my arabic text ?
• what chose C# or C++
• My winform.designer makes problems
• Detect file type?