代码改变世界

保证只运行一个实例应用程序

2012-07-24 16:39  Spring.Guo  阅读(434)  评论(1编辑  收藏  举报

[STAThread]

static void Main()

{

     bool createdNew = false;

     Mutex mutex = new Mutex(true, “CSharpHowTo_SingleInstanceApp”,

                                   out createdNew);

     if (createdNew)

     {

//没有运行实例

          Application.EnableVisualStyles();

          Application.SetCompatibleTextRenderingDefault(false);

          Application.Run(new Form1());

     }

     else

     {           

MessageBox.Show(  “You can only run a single instance of this app!”);

     }

}