• 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

  • 相关阅读:
    【Canvas】(1)---概述+简单示例
    【jQuery】(8)---jquery Ajax
    lastlogon
    windows server core 2016 IIS远程管理的那些坑
    开机手机显示存储空间不足某些系统功能可能无法正常使用,而且无法取消这个界面,导致手机停在这个界面无法操作。
    javascript prototype理解
    微信小程序诡异错误this.setData报错
    转:goproxy和go modules的初步使用
    真机调试No target device的解决(android studio)3.4.1
    unable to access android sdk add-on list的解决
  • 原文地址:https://www.cnblogs.com/feishunji/p/1432986.html
Copyright © 2020-2023  润新知