Limiting Instances
The other day, there was a question on the PLT Scheme list
about
reusing an existing DrScheme Scheme instance when you double
click on a Scheme source file. DrScheme has a non-trivial startup
time, so this is a very good idea. In the good old days (i.e. back
when I did Win16 programming), it was pretty straightforward to
limit a Windows application to a single instance; the OS told you:
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE
hPrevInstance, LPSTR lpCmdLine, int cmdShow) . In Win32,
hPrevInstance is always NULL, so you need some other method. I was
doing a little research on this and found a few good articles:
-
The Officially Sanctioned Way, i.e.
CreateMutex. - Jeff Prosise has an article on the subject.
- Jim Newcomer has essentially the same article in twoplaces.
- Use
FindWindowto locate another instance of your toplevel window. This is problematic for - Use a Global Atom as a flag. This has the disadvantage that you
still need
FindWindowto locate a window handle to set focus to. - Use a variable in shared memory to act as a flag or semaphore
- Use a mutex.
CreateMutexis an atomic operation, so there's no race condition.