/
aprogrammer
/
dotnet-docs
Обзор
Документация
Войти
/
aprogrammer
/
dotnet-docs
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
docs/standard/parallel-programming/snippets/cs/continuations.cs
47 строк
1 KB
David Pine
Address concerns about chaining tasks and lacking continuation examples with .Unwrap() (#19611)
28 июл 2020, 15:00
Не верифицирован
28 июл 2020, 15:00
9685f88
Код
Авторство
О чём код?
using System; using System.Threading; using System.Threading.Tasks; namespace ContinuationExample { class Program { static async Task Main() { using var cts = new CancellationTokenSource(); // UI thread. Hit the right key the first time. _ = Task.Factory.StartNew(() => { Console.WriteLine("Press 'c' to cancel"); if (Console.ReadKey().KeyChar == 'c') { cts.Cancel(); } }); var tasks = new Task<int>[] { new Task<int>(() => 34), new Task<int>(() => 8) }; Task continuation = Task.Factory.ContinueWhenAll( tasks, antecedents => { int answer = antecedents[0].Result + antecedents[1].Result; Console.WriteLine($"The answer is {answer}"); }); tasks[0].Start(); tasks[1].Start(); await continuation; Console.WriteLine("Press any key to exit."); Console.ReadKey(); } } }