示例#1
0
        public static Boolean RegisterMutex([NotNull] String name)
        {
            if (name is null)
            {
                throw new ArgumentNullException(nameof(name));
            }

            if (Mutexes.TryGetValue(name, out SemaphoreMutex mutex))
            {
                mutex.Capture();
                return(true);
            }

            try
            {
                mutex = new SemaphoreMutex(true, name);

                if (Mutexes.TryAdd(name, mutex))
                {
                    return(true);
                }

                mutex.Dispose();
                return(false);
            }
            catch (Exception)
            {
                return(false);
            }
        }
示例#2
0
        public static Boolean CaptureMutex([NotNull] this SemaphoreMutex mutex)
        {
            if (mutex is null)
            {
                throw new ArgumentNullException(nameof(mutex));
            }

            try
            {
                return(mutex.WaitOne(0));
            }
            catch (AbandonedMutexException)
            {
                return(true);
            }
        }