zoukankan      html  css  js  c++  java
  • winform实现只能打开一次

    //在程序的main函数中加入以下代码 
    bool createdNew; 
    System.Threading.Mutex instance = new System.Threading.Mutex(true, "MutexName", out createdNew); 
    if (createdNew) 
    { 
      Application.Run(new LoginForm()); 
      instance.ReleaseMutex(); 
    } 
    else 
    { 
      Application.Exit(); 
    } 
    //还可以写成以下形式,一个窗体只能启动一次 
    Form1 a = new Form1(); 
    bool createdNew; 
    System.Threading.Mutex instance = new System.Threading.Mutex(true, "MutexName", out createdNew); 
    if (createdNew) 
    { 
      a.ShowDialog(); 
      instance.ReleaseMutex(); 
    } 
    else 
    { 
      a.Close(); 
    }
    private static bool HasRunning()
    {
        Process currentProcess = Process.GetCurrentProcess();
        Process[] processCollection = Process.GetProcessesByName(currentProcess.ProcessName);
        foreach (Process p in processCollection)
        {
            if (p.Id == currentProcess.Id)  //检查ID是否相同
            {
                return true;
            }
        }
        return false;
    }
    //预防程序启动多个
    bool newMutexCreated = true;
    using (new Mutex(true, Assembly.GetExecutingAssembly().FullName, out newMutexCreated))
    {
    if (!newMutexCreated)
    {
    MessageBox.Show("程序已启动!请不要启动多个程序");
    System.Environment.Exit(0);
    }
    else
    {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new frmMain());
    }
    }
  • 相关阅读:
    「SDOI2009」Bill的挑战
    「HAOI2011」Problem c
    HDU3530【STL/单调队列/RMQ】
    HDU2874【倍增、ST】
    POJ2955【区间DP】
    SPOJ375 Query on a tree 【倍增,在线】
    训练[2]-DFS
    Fighting For 2017 Season Contest 1
    POJ2796/DP/单调栈
    New Year Tree 【DFS序+线段树区间查询修改+二进制保存状态】
  • 原文地址:https://www.cnblogs.com/glzgc/p/7940397.html
Copyright © 2011-2022 走看看