Note that there are some explanatory texts on larger screens.

plurals
  1. POwhy does Mutex.WaitOne() returns immediately in WPF?
    text
    copied!<p>I am trying to implement SigleInstance application per user session. I am using a local mutex for this. I have the following startup code.</p> <pre><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(); } } } } </code></pre> <p>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 possibly be wrong in this code. Please help</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload