• 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();
                }           
            }
  • 相关阅读:
    Django与Ajax
    Django 常用字段和参数
    Django 模型层之多表操作
    Django 模型层之单表操作
    Django模板层
    Django 视图层
    Django 路由控制
    Django入门(下)
    Django入门(上)
    JavaScript常用类
  • 原文地址:https://www.cnblogs.com/Fred1987/p/11834297.html
Copyright © 2020-2023  润新知