O'Reilly logo

Microsoft® Visual Basic® .NET Programmer's Cookbook by Matthew MacDonald

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required