zoukankan      html  css  js  c++  java
  • C# Mutex to make sure only one unique application instance started

    
    
    static void MutexDemo2()
            {
                string assName = Assembly.GetEntryAssembly().FullName;
                bool createdNew;
                using (var mutex = new Mutex(false, assName, out createdNew))
                {
                    if (!createdNew)
                    {
                        Console.WriteLine("The app in running!");
                        Console.ReadLine();
                    }
                    else
                    {
                        Console.WriteLine("Welcome!");
                        Console.ReadLine();
                    }
                }
            }
    
    
    
    using System;
    using System.Threading.Tasks;
    using System.Reflection;
    using System.Threading;
    using System.Windows.Forms;
    
     static void Main(string[] args)
            {
                MutexDemoBool();
                Console.ReadLine();
            }
    
            static void MutexDemo()
            {
                string assemblyName = Assembly.GetEntryAssembly().FullName;             
                var mutex = new Mutex(false, assemblyName);
    
                if (!mutex.WaitOne(0, true))
                {
                    MessageBox.Show("Unable to run multiple instances of this program.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Console.WriteLine("Welcome");
                    Console.ReadLine();
                }            
            }
    
            static void MutexDemoBool()
            {
                string assemblyName = Assembly.GetEntryAssembly().FullName;
                bool isFirstInstance;
                var mutex = new Mutex(false, assemblyName,out isFirstInstance);
                if(!isFirstInstance)
                {
                    MessageBox.Show("Unable to run multiple instances of this program.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                else
                {
                    Console.WriteLine("Welcome");
                    Console.ReadLine();
                }           
            }
  • 相关阅读:
    一个测试HTML Method的例子
    eXtplorer:在线管理网站文件的利器
    PHPXref:PHP文件交叉引用工具
    统计MySQL数据库大小
    PHP函数glob()
    PclZip:PHP压缩与解压缩
    PHP发送有附件的电子邮件[转]
    查找表中的主键
    CentOS6.4简单配置Cobar
    CentOS6.4 安装mysql cmake的参数说明
  • 原文地址:https://www.cnblogs.com/Fred1987/p/11834297.html
Copyright © 2011-2022 走看看