zoukankan      html  css  js  c++  java
  • C# WinForm程序防止多开的方法总结(亲测)

    1、Winform启动的时候,检测是否存在同样的进程名,防止程序多开;

     1 static class Program
     2     {
     3         /// <summary>
     4         /// 应用程序的主入口点。
     5         /// </summary>
     6         [STAThread]
     7         static void Main()
     8         {
     9             Process[] processes = Process.GetProcesses();
    10             Process currentProcess = Process.GetCurrentProcess();
    11             bool processExist = false;
    12             foreach (Process p in processes)
    13             {
    14                 if (p.ProcessName == currentProcess.ProcessName && p.Id != currentProcess.Id)
    15                 {
    16                     processExist = true;
    17                 }
    18             }
    19 
    20             if (processExist)
    21             {
    22                 Application.Exit();
    23             }
    24             else
    25             {
    26                 Application.EnableVisualStyles();
    27                 Application.SetCompatibleTextRenderingDefault(false);
    28                 Application.Run(new Form1());
    29             }
    30         }
    31     }
     1 static class Program
     2     {
     3         /// <summary>
     4         /// 应用程序的主入口点。
     5         /// </summary>
     6         [STAThread]
     7         static void Main()
     8         {
     9             string processName = Process.GetCurrentProcess().ProcessName;
    10             Process[] processes = Process.GetProcessesByName(processName);
    11             //如果该数组长度大于1,说明多次运行
    12             if (processes.Length > 1)
    13             {
    14                 MessageBox.Show("程序已运行,不能再次打开!");
    15                 Environment.Exit(1);
    16             }
    17             else
    18             {
    19                 Application.EnableVisualStyles();
    20                 Application.SetCompatibleTextRenderingDefault(false);
    21                 Application.Run(new Form1());
    22             }
    23         }
    24     }

    2、利用Mutex互斥对象防止程序多开;

     1 static class Program
     2     {
     3         /// <summary>
     4         /// 应用程序的主入口点。
     5         /// </summary>
     6         [STAThread]
     7         static void Main()
     8         {
     9             bool isAppRunning = false;
    10             Mutex mutex = new Mutex(true, System.Diagnostics.Process.GetCurrentProcess().ProcessName, out isAppRunning);
    11             if (!isAppRunning)
    12             {
    13                 MessageBox.Show("程序已运行,不能再次打开!");
    14                 Environment.Exit(1);
    15             }
    16             Application.EnableVisualStyles();
    17             Application.SetCompatibleTextRenderingDefault(false);
    18             Application.Run(new Form1());
    19         }
    20     }
  • 相关阅读:
    对于数据的测试
    绕过前端,直接调用后端接口的可能性
    API接口自动化之3 同一个war包中多个接口做自动化测试
    API接口自动化之2 处理http请求的返回体,对返回体做校验
    API接口自动化之1 常见的http请求
    DB中字段为null,为空,为空字符串,为空格要怎么过滤取出有效值
    Linux 常用的压缩命令有 gzip 和 zip
    SQL 常用的命令
    JVM内存管理的机制
    Linux 常见命令
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/14277936.html
Copyright © 2011-2022 走看看