Я пытаюсь реализовать приложение SigleInstance для каждого сеанса пользователя.Я использую локальный мьютекс для этого.У меня есть следующий код запуска.
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 всегда верно независимо от того, является ли это первый экземпляр или второй экземпляр.mutex.waitone () возвращается немедленно, без ожидания.Я почесал голову за то, что может быть не так в этом коде.Пожалуйста, помогите