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

    代码
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Threading;
    using System.Windows.Forms;

    namespace Terry{
        
    class Program
        {
            
    private Mutex mutex = null;

            
    public Program()
            {
                mutex 
    = new Mutex(false"TerrySoft_MUTEX");
                
    if (!mutex.WaitOne(0false))
                {
                    mutex.Close();
                    mutex 
    = null;
                }
            }

            
    public void ReleaseMutex()
            {
                
    if (mutex != null)
                {
                    mutex.ReleaseMutex();
                    mutex 
    = null;
                }
            }

            
    ~Program()
            {
                ReleaseMutex();
            }

                    
    /// <summary>
            
    /// The main entry point for the application.
            
    /// </summary>
            [STAThread]
            
    static void Main()
            {

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(
    false);

                Program cm 
    = new Program();
                
    if (cm.mutex == null)
                {
                    MessageBox.Show(
    "已经有一个程序在运行中。");
                }
                
    else
                {
                    Application.Run(
    new frmStart());
                }
                cm.ReleaseMutex();
    //手工释放Mutex

            }

        }
    }
  • 相关阅读:
    deleted
    deleted
    deleted
    deleted
    deleted
    deleted
    POJ 1840 Eqs(乱搞)题解
    UVALive 6955 Finding Lines(随机化优化)题解
    CodeForces 828E DNA Evolution(树状数组)题解
    UVA 11019 Matrix Matcher(二维hash + 尺取)题解
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/1701219.html
Copyright © 2011-2022 走看看