示例#1
0
        public void SingleThread_MultipleAcquireRelease( )
        {
            using (var mutex = new NamedMutex("Global\\TestMutex", null))
            {
                Assert.IsNotNull(mutex, "The mutex should not be null");

                bool isAcquired = mutex.Acquire(0);

                Assert.IsTrue(isAcquired, "The mutex should be acquired");

                mutex.Release( );

                isAcquired = mutex.Acquire(0);
                Assert.IsTrue(isAcquired, "The mutex should be acquired");

                isAcquired = mutex.Acquire(0);
                Assert.IsTrue(isAcquired, "The mutex should be acquired");

                mutex.Release( );
                isAcquired = mutex.Acquire(0);
                Assert.IsTrue(isAcquired, "The mutex should be acquired");

                isAcquired = mutex.Acquire(0);
                Assert.IsTrue(isAcquired, "The mutex should be acquired");

                mutex.Release( );
            }
        }
示例#2
0
        public void SingleThread_InvalidAcquireTimeout( )
        {
            using (var mutex = new NamedMutex("TestMutex", null))
            {
                Assert.IsNotNull(mutex, "The mutex should not be null");

                mutex.Acquire(-10);
            }
        }
示例#3
0
        public void SingleThread_SingleAcquireAutoRelease( )
        {
            using (var mutex = new NamedMutex("TestMutex", null))
            {
                Assert.IsNotNull(mutex, "The mutex should not be null");

                bool isAcquired = mutex.Acquire( );
                Assert.IsTrue(isAcquired, "The mutex should be acquired");
            }
        }
示例#4
0
        public void MultipleThreads_AbandonedMutex( )
        {
            // Create a thread that abandons a mutex
            var t = new Thread(() =>
            {
                //Create a mutex and do not release it.

                var mutex = new NamedMutex("AbandonedMutex");
                mutex.Acquire( );
            });

            t.Start( );
            // Wait for the thread to finish
            t.Join( );

            using (var mutex = new NamedMutex("AbandonedMutex"))
            {
                Assert.IsTrue(mutex.Acquire( ), "Abandoned mutex should be acquired");
            }
        }
示例#5
0
        /// <summary>
        ///     Loads the log entries from the specified log file.
        /// </summary>
        /// <param name="path">
        ///     A string containing the path of the log file to examine.
        /// </param>
        /// <returns>
        ///     A dictionary of the entries contained within the specified log file.
        /// </returns>
        private static EventLogEntryCollection LoadLog(string path)
        {
            var eventLogEntries = new EventLogEntryCollection( );

            // Check that the log file exists
            if (File.Exists(path))
            {
                // Create a mutex for this folder.
                string mutexName = FileEventLogWriter.GetLogMutexName(path);

                using (var logMutex = new NamedMutex(mutexName))
                {
                    if (logMutex.Acquire( ))
                    {
                        eventLogEntries = LoadLog_Impl(path);
                    }
                }
            }

            return(eventLogEntries);
        }
示例#6
0
        public void MultipleThreads_TimedOutMutex( )
        {
            using (AutoResetEvent evt = new AutoResetEvent(false))
                using (AutoResetEvent evt2 = new AutoResetEvent(false))
                {
                    // Create a thread that abandons a mutex
                    var t = new Thread(() =>
                    {
                        using (var mutex = new NamedMutex("TestMutex"))
                        {
                            // Hold the mutex
                            mutex.Acquire( );

                            // ReSharper disable once AccessToDisposedClosure
                            evt2.Set( );

                            // ReSharper disable once AccessToDisposedClosure
                            evt.WaitOne( );
                        }
                    })
                    {
                        IsBackground = true
                    };
                    t.Start( );

                    evt2.WaitOne(500);

                    using (var mutex = new NamedMutex("TestMutex"))
                    {
                        // Attempt to acquire the mutex after waiting 100 ms
                        // This should fail as the thread above is holding the mutex for 10 seconds.
                        Assert.IsFalse(mutex.Acquire(100), "Test mutex should not be acquired");

                        evt.Set( );
                    }
                }
        }
示例#7
0
            /// <summary>
            ///     Thread proc method
            /// </summary>
            private void ThreadProc( )
            {
                _startEvent.WaitOne( );

                bool finished = false;

                while (!finished)
                {
                    using (var mutex = new NamedMutex("TestMutex"))
                    {
                        if (mutex.Acquire( ))
                        {
                            if (_sharedObjectsList.Count == 0)
                            {
                                finished = true;
                            }
                            else
                            {
                                // Get the first object from the list
                                SharedObject so = _sharedObjectsList[0];

                                // If it belongs to this thread task it and remove it from the list
                                if (so.OwnerThread == Thread.CurrentThread.Name)
                                {
                                    _localObjectsDictionary[so.Id] = so;
                                    _sharedObjectsList.RemoveAt(0);
                                }
                            }
                        }

                        if (!_autoRelease)
                        {
                            mutex.Release( );
                        }
                    }
                }
            }