4.18. Ensure That Only One Instance of an Application Can Execute Concurrently

Problem

You need to ensure that a user can have only one instance of an application running concurrently.

Solution

Create a named System.Threading.Mutex object, and have your application try to acquire ownership of it at startup.

How It Works

The Mutex provides a mechanism for synchronizing the execution of threads across process boundaries and also provides a convenient mechanism through which to ensure that only a single instance of an application is running concurrently. By trying to acquire ownership of a named Mutex at startup and exiting if the Mutex cannot be acquired, you can ensure that only one instance of your application is running. Refer to recipe 4-10 ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.