🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.os.ms-windows.programmer.misc
Article #2939

Re: How to avoid multiple instances of a program in NT and 95?

#2939
From: Don Lance
Date: Sat, 08 Feb 1997 00:00
45 lines
2892 bytes
Alois.Buecherl@terraSat.de (Alois Buecherl) wrote:

>Hello,
>I've been striving for so long now to find a SAVE way to start only one 
>instance of my C++ program under Windows NT and 95. As you might recall, this 
>used to be that easy in Windows 3.1 using the prevInstance variable. Now this 
>option is gone.
>I've tried using a semaphore that get's locked when the programm's up. But
>it still can happen, that the program starts up more than once when multiple 
>prgram spawns occur very rapidly. The program then did not get enough 
>startup-time to create and lock the semaphore. 
>Is there ANY solution to this problem?
>
>Any hint into a possibly right direction would be greatly appreciated!
>
>Alois


Here's what I did to solve the problem for one of my apps; it worked
really well.

As someone suggested, create a mutex kernel object that's exclusive for the
app.  When your app starts up, the first thing you should do is look to 
see if it's present.  It is there, then you know you have an instance 
already running.  If it's not, then it's the first instance and proceed
with startup.  

You can enhance your program a little more by switching focus and 
bringing your app to the foreground when someone attempts to start a
second instance.  To handle this, have the app create a "wait" thread to
wait for a created event object to become signalled.  When the event
becomes signalled, the wait thread maximizes its main window, switches
its focus to the foreground, then waits once more.  Note that you should
check your pointer to the apps main window frame, as it could possibly
be NULL during a set of "rapid-fire" clicks from the user.  Since a
thread wait has almost no overhead, you're not chewing up processor time
with it.

Now your app should check for the existance of the mutex, and if present,
signal the previous app's event.  The other app will come to front, while
your new app exits.  You might can optimize this technique a little more 
by using a single kernel object instead of two.

Good luck.
Technical Details
Message-ID: <32fce94e.374683796@isdnlin.mtsu.edu>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <5d4eeb$n6u$1@euler.space.net>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 1124cf,3403ef0501dd273
X-Google-Attributes: gid1124cf,public
X-Google-Thread: 113dfb,3403ef0501dd273
X-Google-Attributes: gid113dfb,public
X-Google-Thread: f6ef4,3403ef0501dd273
X-Google-Attributes: gidf6ef4,public
X-Google-Thread: 105df1,3403ef0501dd273
X-Google-Attributes: gid105df1,public
From: csdon@knuth.mtsu.edu (Don Lance)
Subject: Re: How to avoid multiple instances of a program in NT and 95?
Date: 1997/02/08
Message-ID: <32fce94e.374683796@isdnlin.mtsu.edu>
X-Deja-AN: 217472227
references: <5d4eeb$n6u$1@euler.space.net>
organization: Square D / PowerLogic
reply-to: csdon@knuth.mtsu.edu
newsgroups: comp.os.ms-windows.programmer.misc,comp.os.ms-windows.programmer.win32,de.comp.os.ms-windows.programmer,fido.ger.windows.prog