• 保证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

            }

        }
    }
  • 相关阅读:
    java web 开发 IDE 下载地址
    【转】简述TCP的三次握手过程
    【转】TCP、UDP数据包大小的限制
    复习笔记2018.8.3
    .NET和UNITY版本问题
    LUA全总结
    C++全总结
    C# 全总结
    #region 常量和静态变量静态类readonly
    //todo 的用处
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/1701219.html
Copyright © 2020-2023  润新知