Application instance

Started by
3 comments, last by Scott Bean 23 years, 9 months ago
This is a simple one for you boyz. I''ve got an application which I would like the user to only run once at a time. When launching the application, I need to know if another instance of the application is running. How do I do this? THanks
Advertisement
First, you need to question if you really need to not allow the user to run more than one instance. You should always let the user do what he or she wants, unless it's absoultely necessary that they don't. But if you're really set on it, here's a way you could go about it:
HWND prevHwnd;
prevHwnd = FindWindow(myWndClassName, myWndTitle);
if (prevHwnd)
{
    ShowWindow(prevHwnd, SW_SHOW);
    exit(EXIT_SUCCESS);
}

Edited by - FordPrefect on July 18, 2000 9:17:23 AM
Another way to accomplish this is to use MUTEXes.

-------------------------------------
That's just my 200 bucks!

..-=gLaDiAtOr=-..
Not sure about mutexes, would probably have to keep that thread up the life of the app. but I found an easier way...

if (!GlobalFindAtom("ID-98079879798797666"))
GlobalAddAtom("ID-98079879798797666");
else
{
AfxMessageBox("Only one instance allowed...");
//Exit or whatever
}

Just a little global string (ATOM) holder. Only holds one instance of the queried string.
Well using ATOM''s arent the best way to go as they don''t clean them selfs up or decrement the ref count on application or more importantly on application CRASH. So in testing, if the application crashes, the ATOM remains and next time running the application, I check to see if the atom is existant and yes, it is, so no more instances allowed...

I''m using a dialog based MFC application and don''t know how to access the Modal dialog''s Class Name and Window Name so I can''t check for that.

Ah, maybe I''ll have to use Mutexes... any more suggestions?

Will running a Mutex thread thingy locked throughout the application''s life, decrease speed or give any abnormal behaviors? I''m not too familiar since I dropped out of CompSci a bit too friken early it seems...

This topic is closed to new replies.

Advertisement