Allow Only One Instance of Your Application to Run

Problem

You want to ensure that only one instance of your application can be running at once.

Solution

In the startup code for your application, check the currently running processes to see if your application is already loaded.

Discussion

Limiting your application to a single instance is simply a matter of refusing to start if your application detects that another instance is already present. You can examine currently running processes using the System.Diagnostics.Process class, as described in recipe Find Other Running Processes.

The following Console application provides a simple demonstration. Typically, if you detect more than one running instance, you will simply end the application quietly on ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook 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.