C#Task.continuewith依赖于之前的Task.Result

时间:2014-07-09 20:55:33

标签: c# asynchronous task

我想知道如何使用Task.ContinueWith()方法调用一个Action,其中int是第一个任务的结果。这是相同的代码示例:

Task<int> first_task = GetFirstTask();
Action<int> next_action = (result_from_first) => { //do stuff };
first_task.ContinueWith( () => next_action(first_task.Result) );

错误:

Error   81  Delegate 'System.Action<System.Threading.Tasks.Task<int>>' does not take 0 arguments

1 个答案:

答案 0 :(得分:6)

Task<int> first_task = GetFirstTask();
Action<int> next_action = (result_from_first) => { //do stuff };
first_task.ContinueWith( first => next_action(first.Result) );

应该这样做