周银辉
小经验,做个笔记:
public partial class App : Application
{
public App()
{
//不一定要是在Startup时,请选择适合自己应用程序的合理时间
Startup += AppStartup;
}
void AppStartup(object sender, StartupEventArgs e)
{
bool createNew;
var mutex = new Mutex(true, "TheIdOfThisMutex", out createNew);
if (!createNew)
{
MessageBox.Show("another instance is running");
//记得在关闭程序前关闭mutex,否则进程有可能不退出
mutex.Close();
//不一定要调用Shutdown方法,请选择适合自己应用程序的退出方式
Shutdown();
}
else
{
//记得正常退出程序是关闭mutex
Exit += delegate{ mutex.Close(); };
}
}
}
{
public App()
{
//不一定要是在Startup时,请选择适合自己应用程序的合理时间
Startup += AppStartup;
}
void AppStartup(object sender, StartupEventArgs e)
{
bool createNew;
var mutex = new Mutex(true, "TheIdOfThisMutex", out createNew);
if (!createNew)
{
MessageBox.Show("another instance is running");
//记得在关闭程序前关闭mutex,否则进程有可能不退出
mutex.Close();
//不一定要调用Shutdown方法,请选择适合自己应用程序的退出方式
Shutdown();
}
else
{
//记得正常退出程序是关闭mutex
Exit += delegate{ mutex.Close(); };
}
}
}