Re: [GD-Windows] Limiting to one instance
Brought to you by:
vexxed72
From: Jon W. <hp...@mi...> - 2006-08-15 17:51:45
|
Alen Ladavac wrote: > Personally, I've been using FindWindow(xyz, NULL) to find a main > window by class name. Same principle (with same weaknesses as a named > mutex), but doesn't require an extra object creation, as you probably > already have a window, and you can use this to test even for foreign > Using FindWindow() introduces a possible race condition in your application, because two instances starting up at the same time (because the impatient user double-clicked twice) MAY both first run the test, find that there's no other window, and then create the window. Mutex creation name resolution is guaranteed by the kernel to be atomic, and thus there is no race condition; the first one in, gets it. Note that you don't actually then use the mutex at all; all you do is using the creation of the mutex as your existence test, given that a mutex is reasonably lightweight, and the kernel rules surrounding their lifetime are iron-clad. Of course, if you're worried about pranksters, consider that someone might create a mutex with the same name just to spite you :-P Btw: you don't need to close the handle before exit, because the kernel can clean it up. Trust the kernel. The kernel is your friend! Cheers, / h+ |