每个生成的互斥体名称必须是唯一的。因为每个版本都有不同的程序集名称,所以可以将此名称包含在互斥体的名称中,如下所述。
protected override void OnStartup(StartupEventArgs e)
{
string sMutexUniqueName = "MutexForMyApp" + Assembly.GetExecutingAssembly().GetName().Name;
bool createdNew;
_mutex = new Mutex(true, sMutexUniqueName, out createdNew);
// App is already running! Exiting the application
if (!createdNew)
{
MessageBox.Show("App is already running, so cannot run another instance !","MyApp",MessageBoxButton.OK,MessageBoxImage.Exclamation);
Application.Current.Shutdown();
}
base.OnStartup(e);
//Initialize the bootstrapper and run
var bootstrapper = new Bootstrapper();
bootstrapper.Run();
}