打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
避免多次运行同一个程序进程

 [STAThread]

        static void Main()

        {

            Process Instance = RunningInstance();

            if (Instance == null)

            {

                Application.EnableVisualStyles();

                Application.SetCompatibleTextRenderingDefault(false);

                Application.Run(new Form1());

            }

            else

            {

                MessageBox.Show("条码打印程序已经在运行,请勿多开!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);        

                ActiveForm(Instance);    

            }

        }

        private static Process RunningInstance()

        {

            Process current = Process.GetCurrentProcess();

            Process[] ProcessList = Process.GetProcessesByName(current.ProcessName);

            for (int i = 0; i < ProcessList.Length; i++)

            {

                if (ProcessList[i].Id != current.Id)

                {

                    if (ProcessList[i].MainModule.FileName == current.MainModule.FileName)

                    {

                        return ProcessList[i];

                    }

                }

            }

            return null;

        }

//*********************************************************

 [STAThread]

        public static void Main()
        {

            bool ret;

            System.Threading.Mutex mutex = new System.Threading.Mutex(true, Application.ProductName, out ret);

            if (ret)

            {

                Application.EnableVisualStyles();   //这两行实现   XP   可视风格  

                Application.DoEvents();

                //Main 为你程序的主窗体,如果是控制台程序不用这句

                Application.Run(new 下载通达信数据());

                mutex.ReleaseMutex();

            }

            else

            {

                MessageBox.Show(null, "有一个和本程序相同的应用程序已经在运行,请不要同时运行多个本程序。\n\n这个程序即将退出。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Warning);

                //   提示信息,可以删除。  

                Application.Exit();//退出程序  

            }

        }

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
C#限制程序只能运行一個实例 (防多开)
[C# 开发技巧]如何防止程序多次运行
基于.Net平台应用程序唯一运行实例实现
C#判断程序是否运行的方法
C# 程序自动重启的解决方法
如何使自己的程序只运行一次
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服