Home > Backend Development > C++ > How to Ensure Only One Instance of a WPF Application Runs at a Time?

How to Ensure Only One Instance of a WPF Application Runs at a Time?

Patricia Arquette
Release: 2025-02-02 07:31:09
Original
453 people have browsed it

How to Ensure Only One Instance of a WPF Application Runs at a Time?

Implementing Single-Instance Behavior in WPF Applications

This guide explains how to build a WPF application that allows only one instance to run concurrently. This is achieved using mutexes.

Understanding Mutexes

A mutex (mutual exclusion) is a synchronization primitive. It ensures that only a single process can access a shared resource at any given moment. In WPF, we leverage mutexes to prevent multiple application instances from running simultaneously.

Implementation Details

Utilizing Named Mutexes:

The preferred method involves creating a named mutex within your application's main entry point:

static class Program
{
    static Mutex mutex = new Mutex(true, "{8F6F0AC4-B9A1-45fd-A8CF-72F04E6BDE8F}");
    // ...
}
Copy after login

Named mutexes enable synchronization across multiple processes and threads.

Acquiring the Mutex:

To determine if another instance is already running, we use the WaitOne method:

if (mutex.WaitOne(TimeSpan.Zero, true))
{
    // Mutex acquired; application can start
    Application.Run(new MainWindow());
    mutex.ReleaseMutex();
}
else
{
    // Mutex unavailable; another instance is active
    MessageBox.Show("An instance is already running.");
}
Copy after login

Optional: Notifying the Existing Instance:

For enhanced user experience, you can notify the running instance when a new launch is attempted. This is done using PostMessage:

if (!mutex.WaitOne(TimeSpan.Zero, true))
{
    NativeMethods.PostMessage(
        (IntPtr)NativeMethods.HWND_BROADCAST,
        NativeMethods.WM_SHOWME,
        IntPtr.Zero,
        IntPtr.Zero);
}
Copy after login

Handling the Custom Win32 Message:

In your main window, override WndProc to listen for the custom message and bring the existing window to the foreground:

protected override void WndProc(ref Message m)
{
    if (m.Msg == NativeMethods.WM_SHOWME)
    {
        Activate(); // Bring to front
    }
    base.WndProc(ref m);
}
Copy after login

This comprehensive approach ensures a robust single-instance WPF application, preventing multiple instances from running concurrently. Remember to include necessary NativeMethods definitions (not shown here for brevity).

The above is the detailed content of How to Ensure Only One Instance of a WPF Application Runs at a Time?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template