CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Multi Instances

  1. #1
    Join Date
    May 1999
    Posts
    89

    Multi Instances


    Please help,

    The window application by default allows more than one application instance to run at the same time,
    What shuold i do (MFC) in order to allow only one instance of my window application to run at one time?
    Can somebody pass the code to me if this is easy to be done?

    2) I want to display a status saying "work in progress..please wait" as my GUI and at the same time,
    my program is running behind and get the work done..., how can i do this? do i need multithreading to do this in MFC?

    Thanks!!


  2. #2
    Join Date
    May 1999
    Posts
    16

    Re: Multi Instances

    You can create a mutex with your application class name. Refer to the same mutex before starting a new application. If mutex already exists don't start new application, else start new application.

    Bye,
    Dinesh



  3. #3
    Guest

    Re: Multi Instances


    AddThis function to your App

    BOOL COneInstanceApp::FirstInstance()
    {

    // Allow only one instance
    BOOL bFirstInstance = TRUE;
    HANDLE hMutexOneInstance = NULL;

    #ifdef _WIN32
    hMutexOneInstance = CreateMutex(NULL,TRUE,_T("PreventSecondInstanceACOM"));
    if(GetLastError() == ERROR_ALREADY_EXISTS)
    bFirstInstance = FALSE;
    #else
    if(m_hPrevInstance != NULL)
    bFirstInstance = FALSE;
    #endif

    if(!bFirstInstance){
    CWnd* pFirstWnd;
    if( (pFirstWnd = CWnd::FindWindow(NULL,m_pszAppName)) )
    {
    if(pFirstWnd->IsIconic())
    //pFirstWnd->ShowWindow(SW_SHOWNORMAL);
    pFirstWnd->ShowWindow(SW_RESTORE);
    pFirstWnd->BringWindowToTop();
    pFirstWnd->GetLastActivePopup()->BringWindowToTop();
    }

    }
    #ifdef _WIN32
    if(hMutexOneInstance) ReleaseMutex(hMutexOneInstance);
    #endif
    return bFirstInstance;
    }
    Call it from initInstance
    {


    if (!FirstInstance())
    return FALSE;


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