本文介绍了什么是在C#中使用全局互斥锁的良好格局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Mutex类是非常误解和全球的互斥更是如此。

The Mutex class is very misunderstood, and Global mutexes even more so.

什么是好,安全模式创建互斥量全球时使用的?

What is good, safe pattern to use when creating Global mutexes?

一,将工作


  • 不管语言环境我的机器是在

  • 是保证正确地释放互斥锁

  • 可选如果没有获取互斥不会永远挂

  • 与在其它进程放弃互斥案件优惠

推荐答案

我要确保这是在那里的,因为它是如此难以得到正确的:

I want to make sure this is out there, because it's so hard to get right:

    using System.Runtime.InteropServices;   //GuidAttribute
    using System.Reflection;                //Assembly
    using System.Threading;                 //Mutex
    using System.Security.AccessControl;    //MutexAccessRule
    using System.Security.Principal;        //SecurityIdentifier

    static void Main(string[] args)
    {
        // get application GUID as defined in AssemblyInfo.cs
        string appGuid = ((GuidAttribute)Assembly.GetExecutingAssembly().GetCustomAttributes(typeof(GuidAttribute), false).GetValue(0)).Value.ToString();

        // unique id for global mutex - Global prefix means it is global to the machine
        string mutexId = string.Format( "Global\\{{{0}}}", appGuid );

        // Need a place to store a return value in Mutex() constructor call
        bool createdNew;

        // edited by Jeremy Wiebe to add example of setting up security for multi-user usage
        // edited by 'Marc' to work also on localized systems (don't use just "Everyone")
        var allowEveryoneRule = new MutexAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), MutexRights.FullControl, AccessControlType.Allow);
        var securitySettings = new MutexSecurity();
        securitySettings.AddAccessRule(allowEveryoneRule);

        // edited by MasonGZhwiti to prevent race condition on security settings via VanNguyen
        using (var mutex = new Mutex(false, mutexId, out createdNew, securitySettings))
        {
            // edited by acidzombie24
            var hasHandle = false;
            try
            {
                try
                {
                    // note, you may want to time out here instead of waiting forever
                    // edited by acidzombie24
                    // mutex.WaitOne(Timeout.Infinite, false);
                    hasHandle = mutex.WaitOne(5000, false);
                    if (hasHandle == false)
                        throw new TimeoutException("Timeout waiting for exclusive access");
                }
                catch (AbandonedMutexException)
                {
                    // Log the fact that the mutex was abandoned in another process, it will still get acquired
                    hasHandle = true;
                }

                // Perform your work here.
            }
            finally
            {
                // edited by acidzombie24, added if statement
                if(hasHandle)
                    mutex.ReleaseMutex();
            }
        }
    }

这篇关于什么是在C#中使用全局互斥锁的良好格局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 13:24