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

            }

        }
    }
  • 相关阅读:
    MFC基于对话框的程序添加菜单
    iOS远程推送原理及实现过程
    基于SolrCloud的内容搜索和热点推送
    Android 常用抓包工具介绍之Charles
    帮你快速实现全景应用性能可视化
    Android常用抓包工具之TcpDump
    JAVA IO 序列化与设计模式
    《2015中国移动应用性能管理白皮书》欢迎来看
    利用听云Server和听云Network实测Kubernetes和Mesos在高并发下的网络性能
    魅族电商运维之路
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/1701219.html
Copyright © 2011-2022 走看看