Archived

This topic is now archived and is closed to further replies.

Application instance

This topic is 6357 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites