ユーザーセッションごとにSigleInstanceアプリケーションを実装しようとしています。私はこれにローカルミューテックスを使用しています。私は次のスタートアップコードを持っています。なぜMutex.WaitOne()はすぐにWPFで返されますか?
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は、最初のインスタンスか2番目のインスタンスかにかかわらず常にtrueです。 mutex.waitone()は待機せずにすぐに戻ります。私はこのコードで間違っている可能性があることを頭に傷つけてきました。助けてください
によって作成された場合はfalseを返しますoutパラメータを確認してください。 – Rohit