如果你的意思是多个延续。一个可能的解决方案可能是这样的。
class Program
{
public class TaskState
{
public bool Ended { get; set; }
}
static void Main(string[] args)
{
var task = Task.FromResult("Stackoverflow");
var state = new TaskState();
task.ContinueWith((result, continuationState) =>
{
Console.WriteLine("in first");
}, state).ContinueWith((result, continuationState) =>
{
if (!state.Ended)
Console.WriteLine("in second");
state.Ended = true;
}, state).ContinueWith((result, continuationState) =>
{
if (!state.Ended)
Console.WriteLine("in third");
state.Ended = true;
}, state);
Console.ReadLine();
}
}
我不知道如何在一般情况下有帮助。 “ContinueWith”可能不是最后一个。 –
@JohnSaunders,我不确定我是否理解你的陈述。也许我的例子太蹩脚了。我对创建三个任务的场景感兴趣。第一个完成,因为它开始。第二个(以及所有中间任务)被跳过,因为第三个在第一个完成之前被添加。 – Brannon
也许你的实际问题更好的例子会更有意义。 –