Prevent multiple instances of a given app in .NET?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
If you want only one copy of a desktop .NET application to run at a time, the standard solution is a named mutex. It works across processes, is supported directly by the framework, and is much more reliable than checking process names or window titles.
Why a Named Mutex Is the Usual Answer
A single-instance app needs one process-wide lock that every new launch attempt checks before continuing. A named mutex gives you exactly that.
The basic idea is:
- create or open a mutex with a fixed name
- detect whether your process created it first
- if not, exit or signal the existing instance
A minimal console or WinForms-style example looks like this:
If createdNew is false, another instance already owns that mutex name.
Choose the Mutex Scope Carefully
The mutex name decides how broad the single-instance rule is.
- '
MyCompany.MyAppis usually enough for a per-machine name' - '
Global\MyCompany.MyAppmakes the intent explicit across terminal sessions on Windows' - a user-specific suffix can limit the rule to one instance per logged-in user
That choice matters. Some apps want one instance for the whole machine, while others want one instance per user session.
WPF and WinForms Integration
In a WPF app, the mutex check often belongs near startup before the main window appears.
This prevents the second instance from building the full UI only to realize too late that it should quit.
Bringing the Existing Window to the Front
Preventing the second instance is only half the problem. Good desktop UX often also brings the already-running window forward.
That usually requires inter-process communication such as:
- named pipes
- a local TCP endpoint
- Windows messages for classic desktop apps
The second instance detects the mutex, sends a “show yourself” signal to the first instance, and then exits. The mutex still provides the single-instance guarantee; IPC adds the user-facing polish.
Why Process Enumeration Is Weaker
Checking Process.GetProcessesByName(...) looks tempting, but it is weaker than a mutex. Process names can collide, races are easier, and the logic becomes fragile across renamed executables or multiple install paths.
A named mutex is simpler and more explicit.
Common Pitfalls
The biggest mistake is forgetting that the mutex object must stay alive for the lifetime of the application. If you create it in a short-lived scope and let it be disposed too early, another instance can start.
Another mistake is using an overly generic name that collides with another app or test harness.
A third mistake is assuming “single instance” automatically means the existing window gains focus. That requires extra signaling logic.
Summary
- Use a named mutex to prevent multiple .NET app instances.
- Check the
createdNewflag and exit early in the second instance. - Keep the mutex alive for the full application lifetime.
- Decide whether the rule is per machine, per user, or per session.
- Add IPC only if you also want the existing instance to bring its window forward.
Related reading
- Prevent Visual Studio from automatically creating Mixed Platforms solution configuration
- printing all contents of array in C
- Priority queue in .Net
- Problems creating a Foreign-Key relationship on Entity Framework
- Process.start how to get the output?
- Process.WaitForExit asynchronously
- Produce a random number in a range using C
- Programmatically detecting Release/Debug mode .NET

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.