1 |
Verifying Only One Instance of
the Application is Running
By George Sexton, Rocky Mountain Fox User Group
Ever since the advent of Windows, a popular question has been:
“How do I make sure users only run one instance of my applica-
tion?” While there are a number of :olutions to this problem,
using the Win32 API provides the cleanest. The program below
uses mutexes. Mutex means “Mutually Exclusive”. When
developing Win32 programs that are multi-threaded, mutexes
provide an easy way to synchronize access to a resource. For
example, you have one thread reading data from a device into
an array and another thread that takes the data and performs
some processing. You wouldn’t want both threads altering the
array at the same time. For this case, each thread calls
CreateMutex() to get a handle to the named mutex. The first
thread would call WaitForSingIeObject~to gain ownership of the
mutex. If the second thread were to
try
to get the mutex it would
block until the first thread called ReleaseMutexO. For our
purposes we take advantage of the fact that if you call
CreateMutex() and it already exists, the return value of
GetLastError() is ERROR_ALREADY_EXISTS.
In a nutshell we can have our program open a named mutex. If it
doesn’t exist then the program proceeds normally. If it does
exist, we terminate the program. Any mutexes that a program
owns are released by Windows when the process terminates.
This means that if our program crashes we can restart it without
any problems. As you can see from the implementation pro-
gram using the Win32 API and mutexes we can come up with a
very simple solution to an annoying problem.
‘3/9199 George Sexton
-
MH Software, Inc.
Simple code to keep
two
copies of an application from
executing at me same time.
The code works by creating a mutex. If the mutex doesn’t
exist, tne return coae from GetLastError()
iS U. IT it
does already exist then the return coae is
ERROR_ALREADY_EXISTS
Closing the mutex is not strictly necessary
bUt
SflOWfl TOr
completeness. According to tne
Win32
API
documentation
wnen the owning process terminates the Mutex wiii be
released.
This means that
if
your app errors out and terminates the
user will be able to restart the application.
(Con’t, page 8)
At LA Fox
April
19, 1999, 6:30 PM,
Rod Paddock. Rod Paddock is
president and founder of Dash Point Software, Inc. DPSI based
in Seattle, WA, specializes in developing applications and
training software developers. DPSI develops with Visual lnterdev,
VFP, VB and SQL Server. DPSI’s clients include: ADTC, SBT
Accounting, The US Coast Guard, The US Navy, Pinnacle
Publishing, Intel and Azalea Software. Rod was a featured
spen”3r at
1996/1997 9Q7
German FoxPro DevCons the 1995
and 1998 Visual FoxPro Developers Conference, the 1995-1997
FoxTeach conferences, and the 2nd Annual Southern California
Visual FoxPro Conference. Rod writes for several database
publications including: Data Based Advisor, Foxtalk and Dbase
Advisor. Rod’s books include: VB5 for Web Development and
VFP 5 Enterprise Development from Prima Publishing. Both of
these books have been revised for the Visual Studio 6. DPSI
can
be found at:
http://www.dashpoint.com.
Elsewhere
Conferences play an important and useful part in the FoxPro
community. Here’s a rundown of upcoming events:
Microsoft Tech-Ed 99, May 21-28,1999, Dallas Convention
Center, Dallas, TX. Micosoft Tech-Ed 99 has been expandec
to ive you a choice of three days of focusing on IT operations
management
-
the people who plan, evaluate, deploy, maintair
and support IT systems
-
or five of developer courses for those
who design, develop, or test software applications. For more
info,
go to http://events.microsoft.com/events/teched.
In this issue
Downgrade from VFP?
Inserting/Retrieving Files
Hot Tip
Page
3
Page 3
Page 7
The LA Fox Developer
A
Newsletter for
FoxPro
Application Developers in Southern California
Visit our WebSite at:
http://www.mesllc.com/lafox.html
April 1999
Out and About
by Barry R. Lee
The April meeting of LA Fox will be held on
April 19, 1999, at 6:30 PM at our regular
meeting place (the Torrance Airport, 3301
Airport Drive, in Torrance). For details on
how to get there, see the map on the back
page.
n
and much more
|
1 |