FAQ: How do I make sure that only one instance of my application runs at a time?

Answered FAQ: How do I make sure that only one instance of my application runs at a time?

  • Tuesday, April 14, 2009 10:22 AM
     
     

    How do I make sure that only one instance of my application runs at a time?


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.

All Replies

  • Tuesday, April 14, 2009 10:24 AM
     
     Answered Has Code

    The best way of accomplishing this is using a named mutex. Create the mutex using code such as:


        bool firstInstance;
        Mutex mutex = new Mutex(false, "Local\\" + 
                       someUniqueName, out firstInstance);
        // If firstInstance is now true, we're the first instance of the application;
        // otherwise another instance is running.
    

     

    Note that the mutex is a local one, which means it's in the current user's session. Without the local part, other users would share the mutex, so two different users can’t run the program at the same time. Also note that (unlike various samples around the net) my code doesn't have a call to ReleaseMutex. This is because the mutex will automatically be released when the process dies, which is usually the desired behavior.

     

    One thing to beware of is that the mutex isn't garbage collected. If a local variable is only used near the start of a method, the GC may ignore it when working out which variables are garbage collection "roots" if that part of the method has already been executed. This can lead to the mutex being released earlier than you might anticipate. To prevent this from happening, make a call to GC.KeepAlive (mutex); at the end of your main method. Alternatively, you could use a static variable to store the mutex. This will ensure that the mutex is not garbage collected until the AppDomain is unloaded. (This way, even if Main terminates, you won't have any problems if you've got other threads running.)

     

    Another way of attacking the problem (which comes with its own issues) is to listen on a local port. As only one process can listen on any particular port, this will ensure that no other instance of your application is running. However, if another application wants to use that port as well, either your application will think there's another instance running, or the other application is likely to malfunction.


    Note that this approach has the added benefit of providing a communication channel between the "main" instance and the newly created instance. For example: if the newly created instance wants to tell the main instance to open a file that the user has just requested, it can do so using the socket.

     

    Related threads:

     

    Single app instance

    http://social.msdn.microsoft.com/Forums/en-US/netfxbcl/thread/70aaeed6-6d97-4627-bcbe-9766679f8caf/

    How can I have one and just one copy of my application running at the same time?

    http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/5370ddb4-9b80-4427-9f41-33f345335350/


    Allow only 1 instance of program

    http://social.msdn.microsoft.com/forums/en/netfxbcl/thread/8110c492-de5d-4ba3-a8fd-931a66a069a1/


    How do I prevent the app from running if its already running?!

    http://social.msdn.microsoft.com/forums/en/netfxbcl/thread/249f1978-aa66-495f-95bc-03461ba647c3/

     

    For more FAQ about Basic Class Library, please see Basic Class Library FAQ

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
  • Monday, July 05, 2010 4:36 PM
     
     

    For whatever it might be worth, this is from VS Help:

    Applications created with Visual Basic are multiple-instance applications by default; users can launch multiple instances of the compiled application. In some cases, you may want to prevent users from launching multiple instances of your application. This can be done by setting the Make Single Instance Application property for your project, accessible from the Application page of the Project Designer.

    When the Make Single Instance Application check box is selected, only a single instance of the compiled application is allowed. If a user tries to launch a second instance, focus is shifted to the instance that is already running, and its StartupNextInstance event is raised.

    To create a single-instance application

    1. With a project selected in Solution Explorer, on the Project menu, click Properties.

    2. Click the Application tab.

    3. Select the Make single instance application check box.

    To create a multiple instance application

    1. With a project selected in Solution Explorer, on the Project menu, click Properties.

    2. Click the Application tab.

    3. Clear the Make single instance application check box.

      Note:

      Applications are multiple instances by default.