|
-
December 9th, 2003, 11:32 AM
#1
single app instance - debug mode only?
In the InitInstance method of my App class, I have this code to check for a single instance of my application. I know there might be better ways of doing this, but this suffices for me, or does it?
//create a mutex to keep this app a single instance app
//don't worry about return HANDLE because it's auto reclaimed by OS
//on process termination.
CString mutexError = "";
if (!CreateMutex(NULL, TRUE, "MYAPPTITLE INSTANTIATED"))
{
mutexError = "Unexpected ERROR: MyAppTitle - CreateMutex FAILED !";
AfxMessageBox(mutexError, MB_ICONEXCLAMATION);
return FALSE;
}//end if
if (ERROR_ALREADY_EXISTS == ::GetLastError())
{
CWnd *pWndPrev, *pWndChild;
CString sWinName("MyAppTitle");
if (pWndPrev = CWnd::FindWindow(NULL,sWinName))
{
//if so, does it have any popups?
pWndChild = pWndPrev->GetLastActivePopup();
//if iconic, restore the main window
if (pWndPrev->IsIconic())
{
pWndPrev->ShowWindow(SW_RESTORE);
}//end if
//bring the main window or its popup to the foreground
pWndChild->SetForegroundWindow();
}//end if
return FALSE;
}//end if
Anyway, this code seems to work, but only in Debug mode. When I run debug mode of my application, and then run another instance (double-click the debug executable and/or the C++ developer environment debug), I see another instance startup in Windows Taskmanager, then it goes away.
But in release mode, this code doesn't seem to work? It just creates an instance every time I run in release mode. Any ideas as to why it wouldn't work in release mode, but will work in debug mode?
-
December 9th, 2003, 11:42 AM
#2
nevermind, this code does work in release mode...i was doing something wrong that pertained to some other part of my application, which was why it wasn't working properly...so if need be, this thread can be deleted.
-
December 9th, 2003, 11:58 AM
#3
actually....I thought it was working, because when I would execute the program again, the same window would reappear (if minimized) or would blink if it was already restored on my desktop. But when I look in TaskManager, I see multiple instances of MyAppTitle.exe running, even though I only see one window for my application. Again, this is only in release mode...Any help would be great, thanks.
-
December 9th, 2003, 12:08 PM
#4
Originally posted by elim
actually....I thought it was working, because when I would execute the program again, the same window would reappear (if minimized) or would blink if it was already restored on my desktop. But when I look in TaskManager, I see multiple instances of MyAppTitle.exe running, even though I only see one window for my application. Again, this is only in release mode...Any help would be great, thanks.
I pasted your code into a simple app, and it seems to work just fine in release mode.
Perhaps you could do the same thing I did - put the code into a new project and see if it works as expected. If so, the problem is elsewhere in the original project. If it does not work, post your sample project and someone can take a look at it.
I might note that it is helpful if you use the CODE tags when posting code...
Also - the comment in the code:
//create a mutex to keep this app a single instance app
//don't worry about return HANDLE because it's auto reclaimed by OS
//on process termination.
indicates poor programming practice.
Thought for the day/week/month/year:
Windows System Error 4006:
Replication with a nonconfigured partner is not allowed.
-
December 9th, 2003, 12:18 PM
#5
Yeah, I had another application that I used this code in also, and it works fine with that one as well...So, it's got to be something I'm doing wrong somewhere in my original application.
I'll search around some more and see if I can't come up with the reason why it's not working...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|