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(, 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();
}
}

C# Mutex to make sure only one unique application instance started的更多相关文章

  1. type parameters of <T>T cannot be determined; no unique maximal instance exists for type variable T with upper bounds int,java.lang.Object

    今天在进行代码检查的时候出现下面的异常: type parameters of <T>T cannot be determined; no unique maximal instance ...

  2. 使用Mutex實現單一程式執行個體的注意事項(转)

    相信大家都知道在.NET程式中若要實現單一程式執行個體,一般來說有幾種方法,像是去判斷是否已經有開啟的Process是相同的程式.用Mutex與Semaphore之類的技術來判斷是否程式正在開啟.但是 ...

  3. c# Thread5——线程同步之基本原子操作。Mutex互斥量的使用

    之前的博文也说到了如果多线程对于访问的公共资源操作都是原子操作,那么可以避免竞争条件.关于多线程的竞争可以百度. 1.执行最基本的原子操作 c#提供了一系列供我们使用的原子操作的方法和类型,比如我们的 ...

  4. Write thread-safe servlets [reproduced]

    If you write Web applications in Java, the servlet is your best friend. Whether you write Java Serve ...

  5. WPF实现只打开一个窗口,并且重复打开时已经打开的窗口置顶

    内容来自:https://codereview.stackexchange.com/questions/20871/single-instance-wpf-application 第一步:添加Syst ...

  6. mysql的TABLE_SCHEMA的sql和information_schema表, MySQL管理一些基础SQL语句, Changes in MySQL 5.7.2

    3.查看库表的最后mysql修改时间, 如果第一次新建的表可能还没有update_time,所以这里用了ifnull,当update_time为null时用create_time替代 select T ...

  7. pushlet单播与多播

    近期要弄一个消息推送的功能,在网上找了非常多的关于pushlet的文章,尽管写的都非常具体,可是本人看了以后却总认为是模棱两可···不知道怎样下手,终于參考了这些文章中的一些内容,并结合官网的源码.做 ...

  8. C++关于锁的总结(一)

    C++关于锁的总结(一) 线程中的锁分为两种,互斥锁和共享锁. 相关的头文件有<mutex>,<shared_mutex>,前者具有std::unique_lock操作,用于实 ...

  9. [源码解析] 机器学习参数服务器ps-lite 之(3) ----- 代理人Customer

    [源码解析] 机器学习参数服务器ps-lite 之(3) ----- 代理人Customer 目录 [源码解析] 机器学习参数服务器ps-lite 之(3) ----- 代理人Customer 0x0 ...

随机推荐

  1. jupyter notebook改变行间图片大小

    jupyter notebook使用起来代码效果很直接,这是我最喜欢的一点,但是主题单一,后来改了一下主题.也可以接受了,但是还有一个问题是显示图片太小我们可以用两个方法来改变它. 一.可以通过rcP ...

  2. navicat的一些常用快捷键

    Navicat可以支持连接多种数据库,使用上的功能也比较强大. 如果使用了IDEA内置的数据库工具(个人喜欢用这个)或是SQL Server官方的数据库管理工具的话,会发现使用上都存在区别,区别就主要 ...

  3. Java 添加Word文本水印、图片水印

    水印是一种常用于各种文档的声明.防伪手段,一般可设置文字水印或者加载图片作为水印.以下内容将分享通过Java编程给Word文档添加水印效果的方法,即 文本水印 图片水印 使用工具:Free Spire ...

  4. Java题库——Chapter8 对象和类

    1)________ represents an entity(实体) in the real world that can be distinctly identified. 1) _______ ...

  5. Selenium模块的安装

    Selenium模块 1.安装selenium python2:pip install selenium python3:pip install selenium 2.设置浏览器驱动 解压后必须与浏览 ...

  6. Add an Item to the Navigation Control 将项目添加到导航控件

    In this lesson, you will learn how to add an item to the navigation control. For this purpose, the N ...

  7. SpringCloud(七):springcloud-config统一管理配置中心

    前言: Spring Cloud Config组件是独立的,不需要注册到eureka.config工作原理是把读取目标到配置拉取到本地缓存一份然后供给其他客户端使用,所以一旦config启动成功,可以 ...

  8. Python 中運算子 + 與 * 的 Overload 方法重載功能

    字串 String 原本不能像數值一樣有加減乘除運算,但有例外.例如: >>> 'Good' + 'Morning''GoodMorning' 運算子 + 可將2個字串,串接起來. ...

  9. OpenCV:增加和减少图像的亮度,图像的加减法

    首先导包: import numpy as np import cv2 import matplotlib.pyplot as plt def show(image): plt.imshow(imag ...

  10. 服务器返回的数据将Unicode码转成汉字

    当我们请求接口的时候,服务器会返回一些数据,当我们打印的时候就会发现,打印出来的是unicode码,不是汉字. 这时候需要我们自己手动处理一下,让打印的时候输出汉字的格式. 方法如下: 新增一个分类, ...