using System;
using System.Threading;
using System.Threading.Tasks;
namespace ConsoleApp3
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Passing the result of one task as an input into another");
var task = Task.Factory.StartNew(CallWebService)
.ContinueWith(previousTask => CallStoreProcedure(previousTask.Result));
Console.WriteLine($"Result:{task.Result}");
}
static decimal CallWebService()
{
Console.WriteLine("Starting to call web service...");
Thread.Sleep(new Random().Next(2000, 4000));
Console.WriteLine("Finished call to web service");
return 89.99M;
}
static string CallStoreProcedure(decimal amount)
{
Console.WriteLine("Starting to stored procedure");
Thread.Sleep(new Random().Next(2000, 4000));
Console.WriteLine("Finished call to stored procedure");
return $"12 produects cost more than {amount:C}";
}
}
}
C# 一个Task的输出作为另一个Task的输入
最新推荐文章于 2022-05-28 15:42:07 发布