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

            }

        }
    }
  • 相关阅读:
    jQuery入门和DOM对象
    jQuery事件
    基础,层次,选择器
    MarkDown快速入门(typora)
    source是读入环境配置文件的命令,不能读入vimrc
    vi中将tab键转化为空格
    django-rest-framework学习之Quickstart和Serializer--2017年4月10日至12日
    Flask-RESTful插件介绍--2017年4月7日
    python restful api 编程--2017年4月6日
    一个验证登录的程序:python编写flask架构restful风格--2017年4月6日
  • 原文地址:https://www.cnblogs.com/zitjubiz/p/1701219.html
Copyright © 2011-2022 走看看