Skip to content

Single instance on Windows/Desktop/Mac? #19604

Answered by TopperDEL
TopperDEL asked this question in Q&A
Discussion options

You must be logged in to vote

You are right! I tried this before but it did not work for whatever reason. Now it did.

For anyone curious, here is my solution:

public App()
{
    this.InitializeComponent();

    if (!EnsureSingleInstance())
    {
        BringExistingInstanceToFront();

        // If another instance is already running, exit this one
        Environment.Exit(0);
    }
}

#region Single-Instance
private static bool EnsureSingleInstance()
{
    string appName = "MyApp";

    if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
    {
        // Windows: Use a named Mutex
        _mutex = new Mutex(true, $"Global\\{appName}", out bool isNewInstance);
        return isNewInstance;
    }
    else if (Run…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@TopperDEL
Comment options

Answer selected by TopperDEL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants