コード例 #1
0
        public static void TaskContinuations()
        {
            var task1 = Task.Run(() => IntensiveOperationsService.CancellableSleepingSum(1, 3));
            var task2 = task1.ContinueWith(t => ThreadLogger.Log($"The sum is {t.Result}"));
            var task3 = task2.ContinueWith(t => ThreadLogger.Log("Just a simple message from third task"));

            ThreadLogger.Log("Waiting for simple message from third task");
            //Needed to avoid mixing with other examples
            task3.Wait();
        }
コード例 #2
0
        public static void CancellingTheTask()
        {
            var cts   = new CancellationTokenSource();
            var token = cts.Token;

            var task1 = Task.Run(() => IntensiveOperationsService.CancellableSleepingSum(1, 10, token), token);

            Thread.Sleep(2000);
            ThreadLogger.Log("Cancelling the cts");
            cts.Cancel();
        }
コード例 #3
0
        public static void QueueUserWorkItemAndCancelIt()
        {
            ThreadLogger.Log("main start");

            var cts   = new CancellationTokenSource();
            var token = cts.Token;

            System.Threading.ThreadPool.QueueUserWorkItem(x => IntensiveOperationsService.CancellableLoop(token));

            Thread.Sleep(3000);
            cts.Cancel();
            ThreadLogger.Log("main end");
        }
コード例 #4
0
        public static void WaitAnyAndWaitAll()
        {
            var task1 = Task.Run(() => IntensiveOperationsService.CancellableSleepingSum(1, 1));
            var task2 = Task.Run(() => IntensiveOperationsService.CancellableSleepingSum(2, 2));
            var task3 = Task.Run(() => IntensiveOperationsService.CancellableSleepingSum(3, 3));

            var index = Task.WaitAny(task1, task2, task3);

            ThreadLogger.Log($"Task {index + 1} finished first");

            Task.WaitAll(task1, task2, task3);
            ThreadLogger.Log("All tasks finished");
        }
コード例 #5
0
        public static void CancellationTokenWithCallback()
        {
            ThreadLogger.Log("main start");

            var cts   = new CancellationTokenSource();
            var token = cts.Token;

            token.Register(() => ThreadLogger.Log("token has been cancelled"));

            System.Threading.ThreadPool.QueueUserWorkItem(x => IntensiveOperationsService.CancellableLoop(token));

            Thread.Sleep(3000);
            cts.Cancel();
            ThreadLogger.Log("main end");
        }
コード例 #6
0
        public static void SimpleTaskCreation()
        {
            //Create and schedule a task via static method
            var task1 = Task.Run(() => IntensiveOperationsService.CancellableSleepingSum(1, 2));

            //Create a new task instance and schedule it via .Start() method
            var task2 = new Task <int>(n => IntensiveOperationsService.CancellableSleepingSum(2, (int)n), 2);

            task2.Start();

            //Create a task factory and use .StartNew() method to create and schedule a task
            TaskFactory tf    = new TaskFactory(/*Various options for all tasks created via this factory*/);
            var         task3 = tf.StartNew(() => IntensiveOperationsService.CancellableSleepingSum(3, 2));


            ThreadLogger.Log($"Task 1: {task1.Result}");
            ThreadLogger.Log($"Task 2: {task2.Result}");
            ThreadLogger.Log($"Task 3: {task3.Result}");
        }
コード例 #7
0
        public static void LinkedCancellationTokens()
        {
            ThreadLogger.Log("main start");

            var cts1 = new CancellationTokenSource();

            cts1.Token.Register(() => ThreadLogger.Log("cts1 cancelled"));
            var cts2 = new CancellationTokenSource();

            cts2.Token.Register(() => ThreadLogger.Log("cts2 cancelled"));
            var cts = CancellationTokenSource.CreateLinkedTokenSource(cts1.Token, cts2.Token);

            cts.Token.Register(() => ThreadLogger.Log("linked cts cancelled"));

            System.Threading.ThreadPool.QueueUserWorkItem(x => IntensiveOperationsService.CancellableLoop(cts.Token));

            Thread.Sleep(3000);
            cts2.Cancel();
            ThreadLogger.Log("main end");
        }
コード例 #8
0
        public static void TaskAndChildTasks()
        {
            var parent = new Task <int[]>(() =>
            {
                var results = new int[3];

                new Task(() => results[0] = IntensiveOperationsService.CancellableSleepingSum(1, 1), TaskCreationOptions.AttachedToParent).Start();
                new Task(() => results[1] = IntensiveOperationsService.CancellableSleepingSum(2, 2), TaskCreationOptions.AttachedToParent).Start();
                new Task(() => results[2] = IntensiveOperationsService.CancellableSleepingSum(3, 3), TaskCreationOptions.AttachedToParent).Start();

                return(results);
            });

            parent.ContinueWith(x => ThreadLogger.Log($"Sum of all results: {x.Result.Sum()}"));

            ThreadLogger.Log("Starting parrent task");
            parent.Start();
            //Needed to avoid mixing with other examples
            parent.Wait();
        }
コード例 #9
0
        public static void CancellationTokenCallbackThrowingExceptionsWithAggregation()
        {
            ThreadLogger.Log("main start");

            var cts   = new CancellationTokenSource();
            var token = cts.Token;

            token.Register(() => throw new Exception("foo"));
            token.Register(() => throw new Exception("bar"));

            System.Threading.ThreadPool.QueueUserWorkItem(x => IntensiveOperationsService.CancellableLoop(token));

            Thread.Sleep(3000);
            try
            {
                cts.Cancel(false);
            }
            catch (AggregateException ex)
            {
                ThreadLogger.Log($"main exception: {ex.GetType()} - {ex.InnerExceptions.Select(e => e.Message).Aggregate((a, b) => a + ", " + b)}");
            }

            ThreadLogger.Log("main end");
        }