/*
* this example demonstrates how tasks are chained, using antecedent and continuation
*/
namespace TaskChaining
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
class Program
{
static void Main(string[] args)
{
Task<string> antecedent = Task.Run(() =>
{
Task.Delay(2000); // recommend to use this from Task library if it's a task. Don't use Thread.sleep()
return DateTime.Today.ToShortDateString();
});
Task<string> continuation = antecedent.ContinueWith(x =>
{
return "Today is " + antecedent.Result;
});
Console.WriteLine("this will print before the result");
Console.WriteLine(continuation.Result);
}
}
}
任务链, 前驱任务, 延续任务, 异步编程, 任务延迟
示例展示了如何使用前任务和后续任务链式处理任务,通过Task.Run和ContinueWith方法实现任务的顺序执行。