예제 #1
0
        private void StopThread()
        {
            try
            {
                var start = DateTime.Now;
                while (IsAlive)
                {
                    SetCommand(2);
                    MutexThread?.Join(TimeSpan.FromSeconds(1));

                    if (DateTime.Now - start > TimeSpan.FromSeconds(10))
                    {
                        throw new TimeoutException($"Could not stop {nameof(MutexThread)}, aborting it.");
                    }
                }

                // Successfully stopped the thread.
                return;
            }
            catch (Exception ex)
            {
                // Let it go...
                Logger.LogWarning(ex);
            }

            // Final solution, abort the thread.

            // MutexThread.Abort(); // Not supported on Linux!
        }
예제 #2
0
        public async Task IsAbortable()
        {
            var         source = new CancellationTokenSource();
            MutexThread waiter = MutexThread.Begin(source.Token);

            Assert.False(waiter.Task.IsCompleted);
            source.Cancel();
            await waiter.Task;
        }
예제 #3
0
        public async Task NoPrematureReturn()
        {
            MutexThread waiter = MutexThread.Begin(CancellationToken.None);
            Mutex       m      = new Mutex();

            try
            {
                await waiter.WaitAsync(m);

                var result = await Task.Run(() => Wait(m, 2));

                Assert.False(result.Waited, "Other thread did not wait");
                Assert.InRange(result.TimeTaken.TotalSeconds, 1, 10000);
            }
            finally
            {
                waiter.Release(m);
            }
        }
예제 #4
0
        public async Task SignalReturns()
        {
            MutexThread waiter = MutexThread.Begin(CancellationToken.None);
            Mutex       m      = new Mutex();

            try
            {
                await waiter.WaitAsync(m);

                Task <WaitResult> childTask = Task.Run(() => Wait(m, 2));
                waiter.Release(m);
                var result = await childTask;
                Assert.True(result.Waited, "Other thread should have waited");
                Assert.InRange(result.TimeTaken.TotalSeconds, 0, 1);
            }
            finally
            {
                waiter.Release(m);
            }
        }
예제 #5
0
        public async Task SignalReturnsOnTime()
        {
            MutexThread waiter = MutexThread.Begin(CancellationToken.None);
            var         m      = new Mutex();

            try
            {
                await waiter.WaitAsync(m);

                Task <WaitResult> childTask = Task.Run(() => Wait(m, 10));
                await Task.Delay(MultTime(_step, 4));

                waiter.Release(m);
                WaitResult result = await childTask;
                Assert.True(result.Waited, "Other thread should have waited");
                Assert.InRange(result.TimeTaken, _step, MultTime(_step, 10));
            }
            finally
            {
                waiter.Release(m);
            }
        }
예제 #6
0
        public async Task SignalReturnsOnTime()
        {
            Task        complete;
            MutexThread waiter = MutexThread.Begin(CancellationToken.None, out complete);
            Mutex       m      = new Mutex();

            try
            {
                await waiter.WaitAsync(m);

                Task <WaitResult> childTask = Task.Run(() => Wait(m, 5));
                await Task.Delay(TimeSpan.FromSeconds(2));

                waiter.Release(m);
                var result = await childTask;
                Assert.True(result.Waited, "Other thread should have waited");
                Assert.InRange(result.TimeTaken.TotalSeconds, 1, 5);
            }
            finally
            {
                waiter.Release(m);
            }
        }