zoukankan      html  css  js  c++  java
  • 程序只能运行一个实例

    判断程序是否已经运行,使程序只能运行一个实例有很多方法,下面记录两种,

    方法1:线程互斥

    static class Program
        {
            private static System.Threading.Mutex mutex;

            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                mutex = new System.Threading.Mutex(true, "OnlyRun");
                if (mutex.WaitOne(0, false))
                {
                    Application.Run(new MainForm());
                }
                else
                {
                    MessageBox.Show("程序已经在运行!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Application.Exit();
                }
            }
        }

    这种检测进程的名的方法,并不绝对有效。因为打开第一个实例后,将运行文件改名后,还是可以运行第二个实例。

    方法2:
    static class Program
        {
            /// <summary>
            /// 应用程序的主入口点。
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);

                // get the name of our process
                string p = System.Diagnostics.Process.GetCurrentProcess().ProcessName;
                // get the list of all processes by that name
                System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(p);
                // if there is more than one process
                if (processes.Length > 1)
                {
                    MessageBox.Show("程序已经在运行中", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Application.Exit();
                }
                else
                {
                    Application.Run(new MainForm());
                }
            }
        }

    よろしく,お願いします!
  • 相关阅读:
    禁止修改的消息首部
    文章详情列表接口 小程序及 模拟器 条数 错误 浏览器正确 调大mysql 查询超时时间 同一接口,小程序环境的TTFB相比浏览器约大5倍。
    七牛云霍锴:实时音视频 SDK 设计实践
    java网页数据抓取
    IDEA properties文件中文自动转为ASCII码(properties输入中文乱码问题)
    阿里官方Java代码规范标准《阿里巴巴Java开发手册 终极版 v1.3.0》下载
    从零开始配置Ubuntu20.04 amd64 virtualenvwarapper angr环境 并做ais3_crackme的CFG图
    请求被中止: 未能创建 SSL/TLS 安全通道 解决方案
    Docker
    爬虫
  • 原文地址:https://www.cnblogs.com/cwbo-win/p/3332476.html
Copyright © 2011-2022 走看看