利用C#线程机制实现应用程序的单实例运行

只要你的程序中Main入口是如下的,则你的程序在运行时,在一个时刻只能有一个程序实例,比如Winamp就是这种,当它在运行时,再又击这个程序,是不会再运行一个实例的.

代码很简单

[STAThread]

static void Main( string [] args)

{

bool isExist;

System.Threading.Mutex mutex= new System.Threading.Mutex( true ,"myApp", out isExist);

//这里的myApp是程序的标识,建议换成你的程序的物理路径,这样的话如果在一个操作系统中这个标志不会和其它程序冲突

if(isExist) Environment.Exit(1); //实例已经存在,退出程序

}

Published At
Categories with Web编程
Tagged with
comments powered by Disqus