Thread: Only one instance of application.

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    132

    Only one instance of application.

    I must forbid the running of another instance of my app if itsd already running. I tried if (hInstance == hPrevInstance) but that obviously didnt work.

    How can this be achieved?
    Thanks
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    One way:
    Create a named event using CreateEvent(). When another instance of your app runs, you can detect if the event has been created.
    Code:
    #include <iostream>
    using namespace std;
    
    #include <windows.h>
    
    int main()
    {
        HANDLE appRunningEvent = CreateEventA(0, FALSE, FALSE, "MyApp");
        if (!appRunningEvent)
        {
            cerr << "CreateEvent() failed: " << GetLastError() << endl;
            return 1;
        }//if
    
        if (GetLastError() == ERROR_ALREADY_EXISTS)
        {
            cout << "An instance of this app is already running." << endl;
            return 2;
        }//if
    
        cout << "No other instances running." << endl;
        
        Sleep(30 * 1000);
    
        CloseHandle(appRunningEvent);
    
        return 0;
    }//main
    gg

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Ah sounds logical to me! Cool, thanks. Is this thread safe, though?
    But what if the application crashes before the CloseHandle is called? No other application will be allowed to run, as the event will still exist?
    Last edited by aker_y3k; 06-01-2005 at 10:17 AM.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    When your applications exits (or crashes) windows will free up any resources used by your program.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    132
    Roger, it works, thanks a lot.
    Y3K Network http://www.y3knetwork.com
    Bringing the software of the future on the net.

  6. #6
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    >> Is this thread safe, though?
    Only one thread in your application should be doing this anyways. If two of your processes call CreateEvent() at the same time, one of them should always return ERROR_ALREADY_EXISTS.

    gg

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another question :O need ideas and confirmation
    By Akkernight in forum C++ Programming
    Replies: 7
    Last Post: 02-23-2009, 03:29 PM
  2. Which one is "quicker" ?
    By AloneInTheDark in forum C# Programming
    Replies: 2
    Last Post: 02-08-2008, 09:23 PM
  3. how to make a windows application
    By crvenkapa in forum C++ Programming
    Replies: 3
    Last Post: 03-26-2007, 09:59 AM
  4. Button handler
    By Nephiroth in forum Windows Programming
    Replies: 8
    Last Post: 03-12-2006, 06:23 AM