Since you've already learned all about the APIs behind the mutex class, this should be fairly straightforward. We're going to create a mutex with a set name, and try to enter the mutex in App.Open. If we're able to enter the mutex, that means no other mutex has entered it, so we must be the first instance of the application. However, if we fail to enter the mutex, another instance of the application is already running.
The code looks like this:
[rbcode]
Class App Inherits Application
Private Dim mMutex as Mutex
Sub Open()
// Create the mutex with your application's name
mMutex = new Mutex( "My Spiffy Application" )
// Now try to enter the mutex
if not mMutex.TryEnter then
// Since we failed, someone else has a lock on the
// mutex.
MsgBox "Please only use one instance of My Spiffy Application at a time"
Quit
end if
// At this point, we own the lock on the mutex -- we're the first app
End Sub
Sub Close()
// Release our lock on the mutex
mMutex.Leave
End Sub
End Class[/rbcode]
So this will only allow one instance of your application at a time. However, what if you want the user to be able to open multiple versions of the same application? Say you have v1.0 and v2.0 and you want the user to be able to have both open at the same time, but not two copies of the same version. It's easy! Just change the mutex's name so that it includes version information, like this:
[rbcode]
mMutex = new Mutex( "My Spiffy Application v1.0" )
[/rbcode]
Since the version information is specified, it changes the mutex name with each version and so multiple versions can be run at the same time.
For me, this example perfectly answers a question I had about using a mutex. I knew from VB that you could use a mutex to ensure that only one instance of your app was running. So I figured that with RB's mutex you could do the same thing (and it sure looks easy compared to VB).
Just a quick question though, this mutex API is only RB2005 and up? Or is it in 5.5?
It is RB 2005v1 and up. If you need it in 5.5, you can use declares to get a mutex (on some systems). It's in the WFS, for example.
Here's a generic Win32 question. Some apps open multiple copies of themselves to open multiple documents. Others (RB, my app, etc.) don't, preferring to open multiple documents within one executable space.
Which is the "preferred" way of doing things? What is the origin of this multiple-run system for executables?
When a second copy of my app is launched to open a document, how should I pass that message to the first running copy (IPCSocket?)?
Thanks.
The preferred way from the end-user's point of view is to open in the same address space. If your application is done properly, it'll use up a lot less resources (since pictures, sounds, etc would be shared in the same address space).
From the programmer's point of view, it's "preferred" to open in its own address space. It's usually easier to code. :-P
As for the second question, yes, you'd use IPCSocket -- stay tuned for part two. ;-)
Thank you Aaron this information really helped!
Glad to hear it!
(As for the second question, yes, you’d use IPCSocket)
I am very anxious to see how this is done.