Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello everybody

I have added this code in App.Xaml.cs in my application that I have written to only start it once, unfortunately unsuccessful.
do you have another solution or what am I doing wrong?

Thanks for the answers


What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Threading.Tasks;
using System.Threading;
using System.Windows;



namespace TimeRecorderPro
{
    /// <summary>
    /// Interaktionslogik für "App.xaml"
    /// </summary>
    public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            //Mutex mit eindeutigem Namen (bspw. GUID)
            Mutex mutex = new Mutex(true, "77aca664-ba25-4964-bc05-d7ba57030720");

            //Prüfung, ob Mutex schon länger aktiv ist..
            if (mutex.WaitOne(TimeSpan.Zero, true))
            {
                //Mutex ist gerade gestartet..
                base.OnStartup(e);
            }
            else
            {
                //Mutex läuft bereits längere Zeit..
                MessageBox.Show("Anwendung läuft bereits!");
                //Anwendung beenden
                Environment.Exit(0);
            }

        }
    }
}
Posted
Updated 7-Dec-20 23:32pm
Comments
TheRealSteveJudge 8-Dec-20 4:17am    
What is wrong? I tried your source code and it behaves like this:
When opening another instance the MessageBox is shown.
After closing the MessageBox the second instance is closed.
MMazi 8-Dec-20 4:23am    
But i can start more than once and no messagebox
TheRealSteveJudge 8-Dec-20 4:48am    
What happens if you change your Mutex like this?
Mutex mutex = new Mutex(true, "Global\\77aca664-ba25-4964-bc05-d7ba57030720");
MMazi 8-Dec-20 5:04am    
that has not changed, the application can still be started more than once and no messagebox
TheRealSteveJudge 8-Dec-20 5:10am    
Sorry. I have no more ideas!

1 solution

Try using this constructor for the mutex - Mutex Constructor (System.Threading) | Microsoft Docs[^] and checking the return before proceeding e.g. (NB not tested)
C#
bool createdNew;
mutex = new Mutex(true, "77aca664-ba25-4964-bc05-d7ba57030720", out createdNew);
if(!createdNew)
{
...etc
Also the scope of mutex is your OnStartup method which means you will be disposing of the mutex on exit of that function - try declaring it as a private member of the Application class e.g.
C#
public partial class App : Application
{
        private Mutex mutex = null;
        protected override void OnStartup(StartupEventArgs e)
        {
        ... etc
 
Share this answer
 
v2
Comments
MMazi 8-Dec-20 6:10am    
Thanks CHill60, something has happened but the application cannot be started at all and only the pop-up window appears. I am writing the cod vlt I used it incorrectly

public partial class App : Application
{
private Mutex mutex = null;
//protected override void OnStartup(StartupEventArgs e)

protected override void OnStartup(StartupEventArgs e)
{
bool createdNew;
//Mutex mit eindeutigem Namen (bspw. GUID)
Mutex mutex = new Mutex(true, "77aca664-ba25-4964-bc05-d7ba57030720", out createdNew);
if(!createdNew)

//Prüfung, ob Mutex schon länger aktiv ist..
//if (mutex.WaitOne(TimeSpan.Zero, true))
{
//Mutex ist gerade gestartet..
base.OnStartup(e);
}
else
{
//Mutex läuft bereits längere Zeit..
MessageBox.Show("Anwendung läuft bereits!");
//Anwendung beenden
Environment.Exit(0);
}

}
}
}
CHill60 8-Dec-20 7:29am    
Sorry - I missed out some information! It's the other way around
if(!createdNew)
{
     //Mutex läuft bereits längere Zeit..
     MessageBox.Show("Anwendung läuft bereits!");
     //Anwendung beenden
     Environment.Exit(0);
}
else
{
    base.OnStartup(e);
}
No need (I think) for the mutex.WaitOne
Sorry I can't run or test this properly where I am

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900