Waits for a WaitHandle to become available.
Inheritance: TaskBase
コード例 #1
0
ファイル: WaitTaskTest.cs プロジェクト: isaveu/common
        public void TestCancel()
        {
            using (var waitHandle = new ManualResetEvent(false))
            {
                // Monitor for a cancellation exception
                var  task                    = new WaitTask("Test task", waitHandle);
                bool exceptionThrown         = false;
                var  cancellationTokenSource = new CancellationTokenSource();
                var  waitThread              = new Thread(() =>
                {
                    try
                    {
                        task.Run(cancellationTokenSource.Token);
                    }
                    catch (OperationCanceledException)
                    {
                        exceptionThrown = true;
                    }
                });

                // Start and then cancel the download
                waitThread.Start();
                Thread.Sleep(100);
                cancellationTokenSource.Cancel();
                waitThread.Join();

                exceptionThrown.Should().BeTrue(because: task.State.ToString());
            }
        }
コード例 #2
0
ファイル: WaitTaskTest.cs プロジェクト: nano-byte/common
        public void TestCancel()
        {
            using (var waitHandle = new ManualResetEvent(false))
            {
                // Monitor for a cancellation exception
                var task = new WaitTask("Test task", waitHandle);
                bool exceptionThrown = false;
                var cancellationTokenSource = new CancellationTokenSource();
                var waitThread = new Thread(() =>
                {
                    try
                    {
                        task.Run(cancellationTokenSource.Token);
                    }
                    catch (OperationCanceledException)
                    {
                        exceptionThrown = true;
                    }
                });

                // Start and then cancel the download
                waitThread.Start();
                Thread.Sleep(100);
                cancellationTokenSource.Cancel();
                waitThread.Join();

                exceptionThrown.Should().BeTrue(because: task.State.ToString());
            }
        }
コード例 #3
0
ファイル: WaitTaskTest.cs プロジェクト: isaveu/common
        public void TestWait()
        {
            using (var waitHandle = new ManualResetEvent(false))
            {
                var task       = new WaitTask("Test task", waitHandle);
                var waitThread = new Thread(() => task.Run());
                waitThread.Start();

                Thread.Sleep(100);
                task.State.Should().Be(TaskState.Started);

                waitHandle.Set();
                waitThread.Join();
                task.State.Should().Be(TaskState.Complete);
            }
        }
コード例 #4
0
ファイル: WaitTaskTest.cs プロジェクト: nano-byte/common
        public void TestWait()
        {
            using (var waitHandle = new ManualResetEvent(false))
            {
                var task = new WaitTask("Test task", waitHandle);
                var waitThread = new Thread(() => task.Run());
                waitThread.Start();

                Thread.Sleep(100);
                task.State.Should().Be(TaskState.Started);

                waitHandle.Set();
                waitThread.Join();
                task.State.Should().Be(TaskState.Complete);
            }
        }