Skip to content
Two Ways to Enforce Single-Instance WPF Apps in .NET
Fundamentals 10 min read

Two Ways to Enforce Single-Instance WPF Apps in .NET

This article demonstrates two techniques to prevent multiple instances of a WPF client application: a cross-platform Mutex approach and a .NET Framework-only VB component wrapper that activates the existing window for better UX.

Java Captain
Java Captain
Java Captain

Problem: Unwanted Multiple Instances

When users double-click a desktop shortcut repeatedly, each click launches a new process. This wastes memory and can cause logic errors — for example, concurrent auto-updaters or multiple instances reading/writing the same temporary files.

Solution 1: Mutex-Based Single Instance (Works on .NET Framework & .NET Core)

Delete the default App.xaml and create a custom entry point StartUp.cs with a Main method marked [STAThread]. Use a named System.Threading.Mutex to detect whether this process is the first instance.

const string mutexName = "MyWpfApp";
bool createdNew;
var mutex = new System.Threading.Mutex(true, mutexName, out createdNew);
if (createdNew) {
    var app = new Application();
    var win = new MainWindow();
    app.Run(win);
} else {
    MessageBox.Show("程序已经在运行", "提示信息");
}

Set the project's startup object to the StartUp class via Project Properties → Application → Startup object. When a second instance starts, createdNew is false and a message box appears. The author provides downloadable demos for both WPF and WinForms:

MutexWpfDemo.zip: https://files.cnblogs.com/files/blogs/699532/MutexWpfDemo.zip

MutexWinFormDemo.zip: https://files.cnblogs.com/files/blogs/699532/MutexWinFormDemo.zip

Solution 2: VB Component Wrapper (Better UX, .NET Framework Only)

This approach uses

Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase

, which natively supports single-instance semantics and fires an event when a subsequent launch occurs. The existing window is brought to front instead of showing a dialog.

Step 1: Make MainWindow a Singleton

public partial class MainWindow : Window {
    private MainWindow() { InitializeComponent(); }
    public static MainWindow win;
    public static MainWindow GetMainWindow() {
        if (win == null) win = new MainWindow();
        return win;
    }
    private void Window_Closed(object sender, EventArgs e) { win = null; }
}

The Closed event nullifies the static reference so a new instance can be created after the window closes.

Step 2: Custom Application Class ( WpfApp.cs )

class WpfApp : System.Windows.Application {
    protected override void OnStartup(StartupEventArgs e) { showWindow(); }
    public void showWindow() {
        MainWindow win = MainWindow.GetMainWindow();
        win.Show();
        win.Activate();
        win.Topmost = true;  // force to top
        win.Topmost = false;
    }
}

The Topmost toggle is a practical trick to ensure a covered window pops to the foreground.

Step 3: VB Wrapper ( SingleWrapper.cs )

Add a reference to Microsoft.VisualBasic. The wrapper inherits WindowsFormsApplicationBase and sets IsSingleInstance = true.

class SingleWrapper : Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase {
    public SingleWrapper() {
        this.IsSingleInstance = true;
        this.ShutdownStyle = ShutdownMode.AfterMainFormCloses;
    }
    WpfApp app;
    protected override bool OnStartup(StartupEventArgs eventArgs) {
        app = new WpfApp();
        app.Run();
        return false;
    }
    protected override void OnStartupNextInstance(StartupNextInstanceEventArgs eventArgs) {
        app.showWindow(); // activate existing window
    }
}

Step 4: Entry Point ( StartUp.cs )

class StartUp {
    [STAThread]
    public static void Main(string[] args) {
        SingleWrapper sw = new SingleWrapper();
        sw.Run(args);
    }
}

Set the startup object to this StartUp class. The author notes this method only works on .NET Framework WPF, not .NET Core, and provides demos:

VBWpfDemo.zip: https://files.cnblogs.com/files/blogs/699532/VBWpfDemo.zip

VBWinFormDemo.zip: https://files.cnblogs.com/files/blogs/699532/VBWinFormDemo.zip

Comparison & Recommendation

Mutex : Cross-platform (.NET Framework & .NET Core), simple, but shows a message box on duplicate launch.

VB Wrapper : Superior UX — activates the existing window — but limited to .NET Framework.

The author prefers the VB wrapper for its user experience and hopes Microsoft or the community will bring equivalent support to .NET Core WPF.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

C++desktop developmentMutex.NET FrameworkWPFsingle-instanceWindowsFormsApplicationBase
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.