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

    主要应用System.Diagnostics名字空间中的Process类来实现
    思路,我们在运行程序前,查找进程中是否有同名的进程,同时运行位置也相同,如是没有运行该程序,如果有,就将同名的同位置的程序窗口置前.

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

  • 相关阅读:
    压缩与解压缩
    权限和特殊权限
    用户和组
    bash基础特性
    vim编辑器
    目录及文件操作命令
    ye
    软件包的安装与管理
    磁盘管理
    归档与展开归档
  • 原文地址:https://www.cnblogs.com/jhabb/p/1881450.html
Copyright © 2011-2022 走看看