WPF学习笔记 - 如何用WPF创建单实例应用程序

时间:2023-03-09 20:31:31
WPF学习笔记 - 如何用WPF创建单实例应用程序

使用一个已命名的(操作系统范围的)互斥量。

bool mutexIsNew;

using(System.Threading.Mutex m = new System.Threading.Mulex(true, uniqueName, out mutexIsNew))

{

if(mutexIsNew)

{

//This is the first instance, can run the appliacation

}

else

{

//There is an instance running, exit.     }

}

因为是操作系统范围的,所以需要确定参数uniqueName指定的名字不会被其他应用程序使用。

http://blog.****.net/cs_oldhorse/article/details/6803569