CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2004
    Posts
    244

    Having single instance application

    Hello guys,

    I have a Winforms application written in managed C++. I would like to only have one instance of my application running on a given system at a time. The reason being opening another instance of my form can really mess up the environment I'm working in.

    Here's what I'm using -

    public ref class CSingleInstance
    {
    private:
    //our Mutex member variable
    Mutex ^m_mutex;

    public:
    CSingleInstance(String ^mutexname)
    {
    m_mutex = gcnew Mutex(false, mutexname);
    }
    ~CSingleInstance()
    {
    //release it when the CSingleInstance object is destroyed
    m_mutex->ReleaseMutex ();
    }
    bool IsRunning()
    {

    return !m_mutex->WaitOne(5, true);
    }
    };

    And in my main() -

    //create a mutex with a unique name
    CSingleInstance ^si= gcnew CSingleInstance("{94374E65-7166-4fde-ABBD-4E943E70E8E8}");

    if(si->IsRunning())
    MessageBox::Show("App already running!!");
    else
    // Create the main window and run it
    Application::Run(gcnew Form1("NULL", "NULL"));

    But the problem I'm running into is that, this only works on my laptop. When i install my application on another system, it doesnt work.

    Can anybody help please??

  2. #2
    Join Date
    May 2005
    Location
    Oregon
    Posts
    3,725

    Re: Having single instance application

    Please use Code tag First.Second thing Simply use CreateMutex() Function .The
    CreateMutex function creates or opens a named or unnamed mutex object.If the function succeeds, the return value is a handle to the mutex object. If the named mutex object existed before the function call, the function returns a handle to the existing object and GetLastError returns ERROR_ALREADY_EXISTS. Otherwise, the caller created the mutex.

    If the function fails, the return value is NULL. To get extended error information, call GetLastError.

    for More Detail have a look in MSDN

    Thanx

  3. #3
    Join Date
    Aug 2004
    Location
    Chennai, India.
    Posts
    380

    Re: Having single instance application

    Here is the code sample..

    Code:
           HANDLE hndle = CreateMutex(NULL,FALSE,"TestMutex");
    
           if(GetLastError() != ERROR_ALREADY_EXISTS)
          {	
    	//..Do your task
          }
          else
          {
                    //..One Instance is already running
                    return;
          }

  4. #4
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Having single instance application

    [ redirected ]
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  5. #5
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Having single instance application

    People we are talking here about a managed application, not Win32 API.

    Well, this runs ok on my PC. At the first running, exists is true, at the second (if started in less than 5 seconds) is false, just as expected.
    Code:
    using namespace System;
    using namespace System::Threading;
    
    int main(array<System::String ^> ^args)
    {
    	Mutex^ m = gcnew Mutex( false,"MyMutex" );
    
    	bool exists = m->WaitOne(10, true);
    
    	Console::WriteLine("Application already running {0}", exists);
    
    	Thread::Sleep(5000);
    
        return 0;
    }
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  6. #6
    Join Date
    Sep 2004
    Posts
    244

    Re: Having single instance application

    I figured out that there is one important thing we need to add -

    GC::KeepAlive(mutex) to keep a hold of the mutex as long as the program is running. Now it works perfect.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured