开发者

why does Mutex.WaitOne() returns immediately in WPF?

开发者 https://www.devze.com 2023-02-04 02:00 出处:网络
I am trying to implement SigleInstance application per user session. I am using a local mutex for this. I have the following startup code.

I am trying to implement SigleInstance application per user session. I am using a local mutex for this. I have the following startup code.

public partial class App : Application
{
    private const string applicationName = "EDisc.Client.Application";
    private const string appGUID = "2AE55EA7-B392-42EF-BDFA-3DAFCE2B4B32";
    public App()
    {
        bool isUnique = false;
        using (var mutex = new Mutex(false, appGUID))   //Local mutex is local to the machine,it is per user,If u need a instance per machine,prefix appGUID with global.
        {
            try
            {
                try
                {
                    isUnique = mutex.WaitOne(TimeSpan.FromSeconds(3), true); //wait in case first instance is shutting down.
                    if (isUnique)   //If this is the first process.
                    {
                        this.Navigated += new NavigatedEventHandler(App_Navigated);
                        this.Exit += new ExitEventHandler(App_Exit);
                        AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
                        this.DispatcherUnhandledException += new System.Windows.Threading.DispatcherUnhandledExceptionEventHandler(App_DispatcherUnhandledException);
                    }
                    else      //If process is already running.
                    {
                        LoggingHelper.LogMessage(CommonMessages.ANOTHER_APP_INSTANCE_OPEN, Source.EDiscClientApplication);
                        MessageBox.Show(CommonMessages.CLOSE_OTHER_APP_INSTANCE, CommonMessages.ANOTHER_APP_INSTANCE_OPEN,
                            MessageBoxButton.OK,
                            MessageBoxImage.Exclamation);
                        CloseApplicationInstance();
                        return;
                    }
                }
                catch (AbandonedMutexException)
                {

                }

            }
            finally
            {
                if (isUnique)
                    mutex.ReleaseMutex();
            }
        }

    }
}

isUnique is always true irrespective of whether it is the first instance or second instance. mutex.waitone() returns immediately without waiting. I have been scratting my head for what can po开发者_StackOverflow社区ssibly be wrong in this code. Please help


Your code is releasing the mutex at the end of the App constructor - you keep it acquired only for a few microseconds (while registering those event handlers). You'll have to move the ReleaseMutex() call (and also the Dispose() call done by the using statement) to run at shutdown of your application.


I believe what is happening is that you are releasing the mutex in the finally block of the construtor. When the constructor exits, you are releasing the mutex and hence the next instance of the application can also aquire this mutex. You can alternatively try:

public Mutex( bool initiallyOwned, string name, out bool createdNew )

So you check the out parameter which would return false if the mutex has already been created by another process

0

精彩评论

暂无评论...
验证码 换一张
取 消