From Xojo Documentation
|You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!|
A type of CriticalSection that has operating system-wide scope and is visible to other applications.
Mutex is short for Mutual Exclusion object. It allows several applications to share the same system resource, but not simultaneously. The suggested usage of a Mutex is to create a Mutex with a unique name when the application launches. When the application needs to use the resource, it calls the Enter or TryEnter methods to get a "lock" on the resource and calls the Leave method when it is finished (to release the lock). When you have a lock on a resource, it means you are allowed to use it.
The most common use of a Mutex is to determine whether another instance of your application is currently running in the same user account (not system-wide). You create a named Mutex in the application's open event and then check to see if you can get a lock on it. If the lock fails, then you know there's another instance of your application running. You can also use Mutexes to work with resources that are shared between applications in the same user account (not system-wide) such as a serial port, printer, or some other system device.
You can call Enter or TryEnter multiple times in the same way as with a CriticalSection, including recursive calls.
For web applications, the name value must not be the same as your Application Identifier.
If a thread that has a lock crashes, the lock is not released.
|If you use an IPCSocket that uses SpecialFolder.Temporary to create the IPCSocket file, then the name of the file cannot be the same name as a Mutex (as it also uses that folder).|
Add a property to your app:
In App.Open event handler, you can attempt to create a Mutex.
If Not mMutex.TryEnter Then
MessageBox("You cannot have more than one copy of this app running!")
mMutex = Nil
In App.Close, you can release the Mutex: