Click here to Skip to main content
15,879,239 members
Articles / Programming Languages / C++/CLI
Article

Limiting your .NET apps to a single instance

Rate me:
Please Sign up or sign in to vote.
4.67/5 (11 votes)
3 Nov 20011 min read 221.5K   18   29
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

MC++
__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

MC++
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.

MC++
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


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and 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 experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
Generaldoesn't work in a release build Pin
Anonymous28-Jun-04 12:31
Anonymous28-Jun-04 12:31 
GeneralRe: doesn't work in a release build Pin
MKlucher27-Oct-04 13:25
MKlucher27-Oct-04 13:25 
AnswerRe: doesn't work in a release build Pin
ricecake12-Mar-07 10:50
ricecake12-Mar-07 10:50 
GeneralQuestion on how to add bring to foreground Pin
Steve Messer29-Feb-04 8:14
Steve Messer29-Feb-04 8:14 
GeneralException in ReleaseMutex() in destructor Pin
Serge T11-Mar-03 7:16
Serge T11-Mar-03 7:16 
GeneralRe: Simplest Way To Make Single Instance Application Pin
Nish Nishant13-May-02 23:38
sitebuilderNish Nishant13-May-02 23:38 
GeneralRe: Simplest Way To Make Single Instance Application Pin
14-May-02 0:13
suss14-May-02 0:13 
GeneralRe: Simplest Way To Make Single Instance Application Pin
14-May-02 0:14
suss14-May-02 0:14 
Smile | :) But it is working well.

A.R Gupta
GeneralA better way Pin
4-Mar-02 11:06
suss4-Mar-02 11:06 
GeneralRe: A better way Pin
Ramu Pulipati26-Jun-02 5:08
Ramu Pulipati26-Jun-02 5:08 
GeneralRe: A better way Pin
JacksonBrown196027-Sep-05 3:47
JacksonBrown196027-Sep-05 3:47 
Generalmutex name Pin
Kannan Kalyanaraman9-Nov-01 23:02
Kannan Kalyanaraman9-Nov-01 23:02 
GeneralRe: mutex name Pin
Nish Nishant9-Nov-01 23:05
sitebuilderNish Nishant9-Nov-01 23:05 
GeneralNothing new, see details MS Knowledge base Q243953 Pin
4-Nov-01 15:46
suss4-Nov-01 15:46 
GeneralRe: Nothing new, see details MS Knowledge base Q243953 Pin
Nish Nishant4-Nov-01 16:09
sitebuilderNish Nishant4-Nov-01 16:09 
GeneralRe: Nothing new, see details MS Knowledge base Q243953 Pin
26-Dec-01 4:33
suss26-Dec-01 4:33 
GeneralRe: Nothing new, see details MS Knowledge base Q243953 Pin
Nish Nishant26-Dec-01 16:04
sitebuilderNish Nishant26-Dec-01 16:04 
GeneralRe: Nothing new, see details MS Knowledge base Q243953 Pin
Tom Archer26-Feb-02 17:30
Tom Archer26-Feb-02 17:30 
GeneralRe: Nothing new, see details MS Knowledge base Q243953 Pin
Anonymous7-Oct-02 18:38
Anonymous7-Oct-02 18:38 
GeneralWell Done Pin
ColinDavies3-Nov-01 22:45
ColinDavies3-Nov-01 22:45 
GeneralRe: Well Done Pin
Nish Nishant3-Nov-01 22:49
sitebuilderNish Nishant3-Nov-01 22:49 
GeneralRe: Well Done Pin
Zac Howland4-Jun-02 5:41
Zac Howland4-Jun-02 5:41 
GeneralRe: Well Done Pin
Nish Nishant4-Jun-02 6:03
sitebuilderNish Nishant4-Jun-02 6:03 
GeneralRe: Well Done Pin
Zac Howland4-Jun-02 6:15
Zac Howland4-Jun-02 6:15 
GeneralRe: Well Done Pin
Nish Nishant4-Jun-02 6:43
sitebuilderNish Nishant4-Jun-02 6:43 

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

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