示例#1
0
        public void TestRunTwoConcurrentTasks()
        {
            DummyTaskParameters params1 = new DummyTaskParameters();

            params1.IsBackgroundTask  = true;
            params1.Name              = "DummyTask1";
            params1.RunInParallel     = true;
            params1.UseSeparateThread = true;
            params1.WaitForAbort      = true;

            DummyTask task1 = new DummyTask(params1);

            DummyTaskParameters params2 = new DummyTaskParameters();

            params2.IsBackgroundTask  = true;
            params2.Name              = "DummyTask2";
            params2.RunInParallel     = true;
            params2.UseSeparateThread = true;
            params2.TimeToWait        = 1000; // Wait one second.

            DummyTask task2 = new DummyTask(params2);

            // Create a task manager for the context.
            TaskManager taskManager = new TaskManager("Test");

            taskManager.RunConcurrentTask(task1);
            taskManager.RunConcurrentTask(task2);
            taskManager.WaitForTaskCompletion(task2, s_TaskTimeout);

            // Task 2 should complete first - then abort task1.
            Assert.AreEqual(1, task2.ExecutionCount);
            Assert.AreEqual(1, task1.ExecutionCount);
            Assert.AreEqual(null, task2.LastException);
            Assert.AreEqual(true, task2.CurrentTaskState.TaskCompleted);


            Assert.AreEqual(false, task1.CurrentTaskState.TaskCompleted);
            taskManager.AbortAllConcurrentTasks();
            taskManager.WaitForTaskCompletion(task1, s_TaskTimeout);

            Assert.AreEqual(null, task2.LastException);
            Assert.AreNotEqual(null, task1.LastException);
            Assert.AreEqual(true, task1.CurrentTaskState.TaskCompleted);

            taskManager.Dispose();
        }