Click to See Complete Forum and Search --> : Multi Instances


Kelvin
May 2nd, 1999, 10:58 PM
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!!

dineshsv
May 3rd, 1999, 04:23 AM
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

May 3rd, 1999, 09:50 AM
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;