使用 Task.Run 时获取 continuewith 任务的参考

Get reference of continuewith task when using Task.Run

假设我有

Task t1 = Task.Run(() =>
{
    // do something
}).ContinueWith((t) => {
    int x;
});

我认为 t1 是 ContinuteWith Task 的引用。我怎样才能得到第一个任务的参考?有可能吗?

是否有快捷方式,或者我是否必须使用 new 创建任务,然后单独附加 continuewith?

单独声明 t1

将第一条腿放在括号中。去掉"Task"开头的

将整个事情分配给 t2

正如@toddmo 所提到的,要获得主要 Task 的两个引用及其延续,您可能喜欢这样做:

Task t1 = Task.Run(() => {
    // do something
});
Task t1Continuation = t1.ContinueWith((t) => {
    int x;
});

据我所知,没有方便的方法可以以单行方式实现这一点。另外,请记住,在 ContinueWith lambda 中接收到的对象是第一个任务,您可以在其中检查它是 results or if there where any errors.