Class

Mutex


Description

A type of CriticalSection that has operating system-wide scope and is visible to other applications.

Methods

Name

Parameters

Returns

Shared

Constructor

name As String

Enter

Leave

TryEnter

Boolean

Method descriptions


Mutex.Constructor

Constructor(name as String)

Note

Constructors are special methods called when you create an object with the New keyword and pass in the parameters above.

The name passed will be used to create a file and therefore must be a valid filename for the host operating system.

If you pass an empty string, the Mutex will be identical to a CriticalSection; it will not be visible to other applications. If you pass a name, you can use the name to access the Mutex.

This example creates a Mutex.

Var m As New Mutex("MyMutex")

Mutex.Enter

Enter

Attempts to get a lock on the resource managed by the CriticalSection.

When the call to Enter succeeds, the function returns and your code has exclusive access to the protected resource. If the lock cannot be obtained immediately, Enter will block the current thread from continuing to run. It will wait for the resource to become available.

Enter differs from the Signal method of the Semaphore class in that it can be called multiple times from the currently executing thread. If the calling thread already owns the lock, the method returns immediately. This makes CriticalSections very useful for calling a method recursively.


Mutex.Leave

Leave

Call Leave when you are finished using the protected resource and you want to give it back to the CriticalSection.

Every time you call Enter or TryEnter and succeed, you must call Leave. This includes calling it recursively. Otherwise the resource will be protected indefinitely.


Mutex.TryEnter

TryEnter As Boolean

Attempts to get a lock on the resource managed by the CriticalSection.

TryEnter is similar to Enter but returns a Boolean. If it succeeds, it returns True and the thread has exclusive use of the resource. If it fails, it returns False but does not block execution of the thread.

Notes

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 Opening 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.

Warning

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).

Sample code

Add a property to your app:

mMutex As Mutex

In the App.Opening event handler, you can attempt to create a Mutex.

mMutex = New Mutex("MutexExample")

If Not mMutex.TryEnter Then
  MessageBox("You cannot have more than one copy of this app running!")
  mMutex = Nil
  Quit
End If

In App.Closing event handler, you can release the Mutex:

If mMutex <> Nil Then
  mMutex.Leave
End If

Compatibility

All project types on all supported operating systems.