実行中のアプリケーションのインスタンスを1つだけ許可するために、mutexを使用しています。コードを以下に示します。これは正しい方法ですか?コードに欠陥はありますか?
ユーザーが2回目にアプリケーションを開こうとしたときに、すでに実行中のアプリケーションを表示する方法。現在(以下のコードで)、別のインスタンスがすでに実行されているというメッセージを表示しています。
static void Main(string[] args)
{
Mutex _mut = null;
try
{
_mut = Mutex.OpenExisting(AppDomain.CurrentDomain.FriendlyName);
}
catch
{
//handler to be written
}
if (_mut == null)
{
_mut = new Mutex(false, AppDomain.CurrentDomain.FriendlyName);
}
else
{
_mut.Close();
MessageBox.Show("Instance already running");
}
}