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

            }

        }
    }
  • 相关阅读:
    简单命令行总结
    [大餐]开发摘记1--我的Fragment通信的框架 | 卖牙膏的芖口钉
    DZNEmptyDataSet的使用
    Java笔记(一)
    mingster.com
    2014新年福利,居然有人将Ext JS 4.1的文档翻译了
    【翻译】Sencha Touch 2入门:创建一个实用的天气应用程序之三
    【翻译】在Ext JS应用程序中使用自定义图标
    【翻译】Siesta事件记录器入门
    【翻译】使用新的Sencha Cmd 4命令app watch
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/1701219.html
Copyright © 2011-2022 走看看