查看: 5557|回复: 0

在VC#下利用互斥阻止同进程多实例运行的正确代码

[复制链接]
发表于 2009-4-3 00:02:45 | 显示全部楼层 |阅读模式
关键词: 代码 , 进程 , 实例 , 运行
在VC#下利用互斥阻止同进程多实例运行的正确代码using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Threading;//Mutex类需要
namespace WindowsFormsApplication2
{
    static class Program
    {
        ///
        /// 应用程序的主入口点。
        ///

        [STAThread]
        static void Main()
        {
            bool createdNew;
            Mutex mutexFile = new Mutex(false, "WindowsFormsApplication2", out createdNew);//创建互斥
            if (createdNew)
            {//成功创建无进程运行
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1());
//              mutexFile.ReleaseMutex();//不应该调用此函数
                mutexFile.Close();//释放句柄
            }
            else
            {//创建失败有进程在运行
                MessageBox.Show("对不起,有一进程在工作", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }
    }
}

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关于我们  -  服务条款  -  使用指南  -  站点地图  -  友情链接  -  联系我们
电子工程网 © 版权所有   京ICP备16069177号 | 京公网安备11010502021702
快速回复 返回顶部 返回列表