Click here to Skip to main content
11,479,170 members (44,277 online)
Click here to Skip to main content

Limiting your .NET apps to a single instance

, 3 Nov 2001 190.3K 18
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

Introduction

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
{
private:
    //our Mutex member variable
    Mutex *m_mutex;	
public:
    CSingleInstance(String *mutexname)
    {
        m_mutex=new Mutex(false,mutexname);
    }
    ~CSingleInstance()
    {
        //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}");
    if(si->IsRunning())
        MessageBox::Show("Already running...so exiting!");
    else
        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.

License

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

Share

About the Author

Nish Nishant

United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored
C++/CLI in Action for Manning Publications in 2005, and had previously co-authored
Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his
WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : You can reach Nish on his google email id voidnish.

Website and Blog

Comments and Discussions

 
GeneralRe: A better way Pin
Jay Giganti27-Sep-05 4:47
memberJay Giganti27-Sep-05 4:47 
Generalmutex name Pin
Kannan Kalyanaraman10-Nov-01 0:02
memberKannan Kalyanaraman10-Nov-01 0:02 
GeneralRe: mutex name Pin
Nish [BusterBoy]10-Nov-01 0:05
memberNish [BusterBoy]10-Nov-01 0:05 
GeneralNothing new, see details MS Knowledge base Q243953 Pin
MN4-Nov-01 16:46
memberMN4-Nov-01 16:46 
GeneralRe: Nothing new, see details MS Knowledge base Q243953 Pin
Nish [BusterBoy]4-Nov-01 17:09
memberNish [BusterBoy]4-Nov-01 17:09 
GeneralRe: Nothing new, see details MS Knowledge base Q243953 Pin
DenaliChief26-Dec-01 5:33
memberDenaliChief26-Dec-01 5:33 
GeneralRe: Nothing new, see details MS Knowledge base Q243953 Pin
Nish [BusterBoy]26-Dec-01 17:04
memberNish [BusterBoy]26-Dec-01 17:04 
GeneralRe: Nothing new, see details MS Knowledge base Q243953 Pin
Tom Archer26-Feb-02 18:30
memberTom Archer26-Feb-02 18:30 
GeneralRe: Nothing new, see details MS Knowledge base Q243953 Pin
Anonymous7-Oct-02 19:38
sussAnonymous7-Oct-02 19:38 
GeneralWell Done Pin
Colin Davies3-Nov-01 23:45
memberColin Davies3-Nov-01 23:45 
GeneralRe: Well Done Pin
Nish [BusterBoy]3-Nov-01 23:49
memberNish [BusterBoy]3-Nov-01 23:49 
GeneralRe: Well Done Pin
Zac Howland4-Jun-02 6:41
memberZac Howland4-Jun-02 6:41 
GeneralRe: Well Done Pin
Nish - Native CPian4-Jun-02 7:03
memberNish - Native CPian4-Jun-02 7:03 
GeneralRe: Well Done Pin
Zac Howland4-Jun-02 7:15
memberZac Howland4-Jun-02 7:15 
GeneralRe: Well Done Pin
Nish - Native CPian4-Jun-02 7:43
memberNish - Native CPian4-Jun-02 7:43 
GeneralRe: Well Done Pin
Zac Howland4-Jun-02 7:58
memberZac Howland4-Jun-02 7:58 
GeneralRe: Well Done Pin
Nish - Native CPian4-Jun-02 8:15
memberNish - Native CPian4-Jun-02 8:15 
GeneralRe: Well Done Pin
Zac Howland4-Jun-02 8:56
memberZac Howland4-Jun-02 8:56 
GeneralRe: Well Done Pin
Anonymous30-Jul-02 6:43
sussAnonymous30-Jul-02 6:43 

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.150520.1 | Last Updated 4 Nov 2001
Article Copyright 2001 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid