How to check if an application is already running

Hi,

I want to keep user from starting two instances of my application at the same time so how can I find if an instance of my application is already running before I start another

Thanks,



Answer this question

How to check if an application is already running

  • kolya

    Thanks,

    I am doing this

    Dim SingleInstance As System.Threading.Mutex = New System.Threading.Mutex(False, Application.ProductName.ToString)

    If Not SingleInstance.WaitOne(0, False) Then

    MessageBox.Show("Another instance of the Application already running")

    Application.Exit()

    End If

    but that does'nt work
    Am I passing correct arguments in Mutex constructor

    Thanks,


  • ctony

  • Stefan Slapeta

    I've used the following technique in C#:
    . Create a mutex using the name of the application, or some similiar key.
    . Try to acquire the mutex using WaitOne().
    . If successful, run.
    . Else, another instance is running so exit.

    For example:

    using (System.Threading.Mutex singleInstance = new System.Threading.Mutex(
       false, "SingleInstance - Wallpaper Changer"))
    {
       if (singleInstance.WaitOne(0, false))
       {
          // App specific setup.
          
    Application.Run();
       }
    }

    For a full working example, see this blog entry, which links to a simple sample:
    http://www.srtsolutions.com/public/item/103695

    Bill Wagner



  • How to check if an application is already running