5,696,038 members and growing! (14,078 online)
Email Password   helpLost your password?
Languages » C++ / CLI » General     Intermediate

Limiting your .NET apps to a single instance

By Nishant Sivakumar

Shows how to use the Mutex class to limit your app to a single instance using a named mutex
C++/CLI, Windows, .NET 1.0, .NET, Visual Studio, Dev

Posted: 3 Nov 2001
Updated: 3 Nov 2001
Views: 148,083
Bookmarked: 12 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
51 votes for this Article.
Popularity: 6.96 Rating: 4.08 out of 5
1 vote, 11.1%
1
0 votes, 0.0%
2
1 vote, 11.1%
3
0 votes, 0.0%
4
7 votes, 77.8%
5

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

About the Author

Nishant Sivakumar


Sitebuilder, Mvp
Nish is a real nice guy living in Atlanta, who has been coding since 1990, when he was 13 years old. Originally from sunny Trivandrum in India, he recently moved to Atlanta from Toronto and is a little sad that he won't be able to play in snow anymore.

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 - www.voidnish.com 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 - blog.voidnish.com

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.
Location: United States United States

Other popular C++ / CLI articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 29 (Total in Forum: 29) (Refresh)FirstPrevNext
Generaldoesn't work in a release buildsussAnonymous13:31 28 Jun '04  
GeneralRe: doesn't work in a release buildsupporterKluch14:25 27 Oct '04  
AnswerRe: doesn't work in a release buildmemberricecake11:50 12 Mar '07  
GeneralQuestion on how to add bring to foregroundmembersmesser9:14 29 Feb '04  
GeneralException in ReleaseMutex() in destructorsussSerge T8:16 11 Mar '03  
GeneralRe: Simplest Way To Make Single Instance ApplicationmemberNish [BusterBoy]0:38 14 May '02  
GeneralRe: Simplest Way To Make Single Instance ApplicationmemberAnonymous1:13 14 May '02  
GeneralRe: Simplest Way To Make Single Instance ApplicationmemberA.R Gupta1:14 14 May '02  
GeneralA better waymemberAndy Smith12:06 4 Mar '02  
GeneralRe: A better waymemberRamu Pulipati6:08 26 Jun '02  
GeneralRe: A better waymemberJay Giganti4:47 27 Sep '05  
Generalmutex namememberKannan Kalyanaraman0:02 10 Nov '01  
GeneralRe: mutex namememberNish [BusterBoy]0:05 10 Nov '01  
GeneralNothing new, see details MS Knowledge base Q243953memberMN16:46 4 Nov '01  
GeneralRe: Nothing new, see details MS Knowledge base Q243953memberNish [BusterBoy]17:09 4 Nov '01  
GeneralRe: Nothing new, see details MS Knowledge base Q243953memberDenaliChief5:33 26 Dec '01  
GeneralRe: Nothing new, see details MS Knowledge base Q243953memberNish [BusterBoy]17:04 26 Dec '01  
GeneralRe: Nothing new, see details MS Knowledge base Q243953memberTom Archer18:30 26 Feb '02  
GeneralRe: Nothing new, see details MS Knowledge base Q243953sussAnonymous19:38 7 Oct '02  
GeneralWell DonememberColin Davies23:45 3 Nov '01  
GeneralRe: Well DonememberNish [BusterBoy]23:49 3 Nov '01  
GeneralRe: Well DonememberZac Howland6:41 4 Jun '02  
GeneralRe: Well DonememberNish - Native CPian7:03 4 Jun '02  
GeneralRe: Well DonememberZac Howland7:15 4 Jun '02  
GeneralRe: Well DonememberNish - Native CPian7:43 4 Jun '02  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 3 Nov 2001
Editor: Nishant Sivakumar
Copyright 2001 by Nishant Sivakumar
Everything else Copyright © CodeProject, 1999-2008
Web16 | Advertise on the Code Project