zoukankan      html  css  js  c++  java
  • Singleinstance application in .NET

    Although there are many resources online, I just want to add a bookmark at my own place.

    A customer used Microsoft.VisualBasic.ApplicationServices to facilitate a single-instance C# application.
    He wants the same applications to run as single instance respectively in different directories.
    Microsoft.VisualBasic.ApplicationServices way is to use the GUID from AssmeblyInfo to uniquely identify the application at the build time.
    So the key point here is to find out a "runtime GUID".

    At start, I was thinking of twist .NET remoting to achieve this.
    But after reading several articles, the idea of using System.Threading.Mutex to consume a "runtime GUID" proves to be a simple and elegant solution.

    A Basic support for single-instance applications is fairly straightforward, and there are a variety of approaches to handling this. All of them involve some sort of shared resource that instances of the application can create and lock. If a second instance of an application is not successful in doing so, it knows that a previous instance already exists and that it should exit.
    The most common shared resource used in managed applications for the creation of single-instance applications is a mutex. A mutex is a synchronization primitive provided by the operating system that allows for interthread and interprocess communication. A mutex is typically used to protect a shared resource, providing exclusive access to it.
    When one thread acquires the mutex, no other threads will be able to acquire it until the thread that did so originally releases it. As such, applications can use a mutex to provide single-instance functionality. On startup, the application will attempt to create and acquire a mutex with a known name. If it can, then it is the first instance. If it can't, then another instance of the application has already created and acquired the mutex, thus alerting this new instance to the fact that it is a duplicate and should exit.
    In the .NET Framework, a mutex is represented through the System.Threading.Mutex class. This class derives from WaitHandle and is a thin wrapper around the kernel's mutex object, which can be created using the Win32® CreateMutex function and opened using the Win32 OpenMutex function.
    First, you need to come up with a name for the mutex. The use of a name allows multiple instances of the application to create or access the same mutex instance, so choosing a unique name ahead of time is important. Some typical examples of names used for this purpose include the name of the entry assembly for the application (available from [Assembly].GetEntryAssembly().FullName), the entry assembly's type library GUID (accessed with Marshal.GetTypeLibGuidForAssembly), or a predefined GUID used to uniquely identify the application. The name can also be autogenerated at runtime, but the algorithm for doing so must be created in such a way that all instances of the same application will generate the same name. With the name in hand, a mutex is instantiated using the constructor that accepts three arguments.
     
    The following code ensures a single-instance of the C#.NET Winform application:
     
    Code version 1
    Edit the next day:
    Considering the possibility that the path could be too long for mutexName, I decide to hash the path first and append Major,Minor version number to the mutexName.
    As such, here is the additional code example:
    Hash path
     

    By the way, the code above needs to add more references:

    using System.Threading;

    using System.Security.Cryptography;

    using System.Text;

    using System.Runtime.InteropServices;

    using System.Reflection;

    Reference:

    http://msdn.microsoft.com/en-us/magazine/cc163741.aspx

    http://www.codeproject.com/KB/cs/CSSIApp.aspx

    http://blogs.msdn.com/onoj/archive/2004/06/04/148532.aspx

  • 相关阅读:
    权限设计=功能权限+数据权限
    C# 自定义配置文件
    asp.net core 系列 2 启动类 Startup.CS
    asp.net core 系列 1 概述
    IOC : Unity 配置和使用
    Unity Ioc 依赖倒置及Untity AOP被动拦截/自动拦截
    webuploader-异步切片上传(暂不支持断点续传)及 下载方法!C#/.NET
    C# 构造基础返回值类型-BaseResponse
    神奇的选择器 :focus-within
    妙用 scale 与 transfrom-origin,精准控制动画方向
  • 原文地址:https://www.cnblogs.com/feishunji/p/1432986.html
Copyright © 2011-2022 走看看