zoukankan      html  css  js  c++  java
  • 保证应用程序只有一个实例运行

            public static System.Diagnostics.Process RunningInstance()
            {
                System.Diagnostics.Process current = System.Diagnostics.Process.GetCurrentProcess();
                System.Diagnostics.Process[] processes = System.Diagnostics.Process.GetProcessesByName(current.ProcessName);
                //查找相同名称的进程
                foreach (System.Diagnostics.Process process in processes)
                {
                    //忽略当前进程
                    if (process.Id != current.Id)
                    {
                        //确认相同进程的程序运行位置是否一样.
                        if (System.Reflection.Assembly.GetExecutingAssembly().Location.Replace("/", "\") == current.MainModule.FileName)
                        {
                            //Return the other process instance.
                            return process;
                        }
                    }
                }
                //No other instance was found, return null.
                return null;
            }

    在main中调用

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                System.Diagnostics.Process instance = RunningInstance();
                if (instance == null)
                {
                    Application.Run(new Form1());
                }
                else
                {
                    MessageBox.Show("已有程序打开");
                }

     示例二

            public static bool ExistOtherOne()
            {
                Process process = Process.GetCurrentProcess();
                Process[] processes = System.Diagnostics.Process.GetProcessesByName(process.ProcessName);
                if (processes.Length > 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
    本博客有部分内容来自网络,如有问题请联系:hebeilijianghua@qq.com,并注明来自博客园。
  • 相关阅读:
    网站优化,dns预解析,解析缓存
    dos命名重启或关闭远程服务器
    IIS 常见问题集记录
    EF 基础提供程序在 Open 上失败
    flexbox学习
    svn post-commit 同步
    备份
    log4net 2.0.4有问题,AdoNetAppender会报错
    signalr 配置错误跟踪
    Facebook的Web开发三板斧:React.js、Relay和GraphQL
  • 原文地址:https://www.cnblogs.com/leebokeyuan/p/9328256.html
Copyright © 2011-2022 走看看