Click here to Skip to main content
Click here to Skip to main content

Limiting your .NET apps to a single instance

, 3 Nov 2001
Rate this:
Please Sign up or sign in to vote.
Shows how to use the Mutex class to limit your app to a single instance using a named mutex


Sometimes you'd want to limit your application to a single instance. In Win32 we had the CreateMutex API function using which we could create a named mutex and if the call failed, we assume that the application is already running. Well the .NET SDK gives us the Mutex class for inter-thread/inter-process synchronization. Anyway in this article we are more interested in using the Mutex class to limit our apps to a single instance rather than in its use as an inter-process data synchronization object.

The Class

The code below is nothing new. It's simply a .NET version of a universal technique that has been used pretty much successfully over the years. For a thorough understanding of this technique and other techniques and issues involved when making single instance applications, you must read Joseph M Newcomer's article - Avoiding Multiple Instances of an Application

__gc class CSingleInstance
    //our Mutex member variable
    Mutex *m_mutex;	
    CSingleInstance(String *mutexname)
        m_mutex=new Mutex(false,mutexname);
        //we must release it when the CSingleInstance object is destroyed
        m_mutex->ReleaseMutex ();
    bool IsRunning()
        //you can replace 10 with 1 if you want to save 9 ms
        return !m_mutex->WaitOne(10,true);           

Using it in your program

int __stdcall WinMain()
    //create a mutex with a unique name   
    CSingleInstance *si=
        new CSingleInstance("{94374E65-7166-4fde-ABBD-4E943E70E8E8}");
        MessageBox::Show("Already exiting!");
        Application::Run(new MainForm());
    return 0;

Remember to put the following line on top of your program.

using namespace System::Threading;

I have used the string {94374E65-7166-4fde-ABBD-4E943E70E8E8}as my unique mutex name. You can use a name that you believe will be unique to your application. Using a GUID would be the smartest option obviously. You can put the class in a DLL and thus you can use it from all your applications.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Nish Nishant

United States United States
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff -
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Comments and Discussions

Generaldoesn't work in a release build PinsussAnonymous28-Jun-04 13:31 
GeneralRe: doesn't work in a release build PinmemberKluch27-Oct-04 14:25 
AnswerRe: doesn't work in a release build Pinmemberricecake12-Mar-07 11:50 
GeneralQuestion on how to add bring to foreground Pinmembersmesser29-Feb-04 9:14 
GeneralException in ReleaseMutex() in destructor PinsussSerge T11-Mar-03 8:16 
Thanks for the article, but I found out the following problem:
When I run my app under debugger (Visual Studio .NET) when I exiting the application, the ApplicationException is being throwed in ReleaseMutex() with such msg: Attempt to release mutex not owned by caller.
My code is in C#, not managed C++, but it's actually the same:

public class SingleInstance
private Mutex m_mutex;
public SingleInstance(string sMutexName)
m_mutex = new Mutex(false, sMutexName);
public bool IsRunning()
return !m_mutex.WaitOne(10, true);

The call is made as:
static void Main()
SingleInstance si = new SingleInstance("My App");
if (si.IsRunning())
MessageBox.Show("Already running!");
Application.Run(new frmMainWnd());

If I run the code not from debugger, no exception!Confused | :confused:
Does anybody have an idea?
GeneralRe: Simplest Way To Make Single Instance Application PinmemberNish [BusterBoy]14-May-02 0:38 
GeneralRe: Simplest Way To Make Single Instance Application PinmemberAnonymous14-May-02 1:13 
GeneralRe: Simplest Way To Make Single Instance Application PinmemberA.R Gupta14-May-02 1:14 
GeneralA better way PinmemberAndy Smith4-Mar-02 12:06 
GeneralRe: A better way PinmemberRamu Pulipati26-Jun-02 6:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150327.1 | Last Updated 4 Nov 2001
Article Copyright 2001 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid