我如何能生产任务&LT;任务&GT;解开 [英] How can I produce a Task<Task> to Unwrap
问题描述
有人能解释一下这两个语句之间的区别:
任务&LT;任务&GT; bTask = backup.BackupCurrentDatabaseAsync()
.ContinueWith(_ =&GT;的COM pressArchiveAsync());
//拆开包装的任务,以产生一个完整的任务
任务T = bTask.Unwrap();
VS
任务&LT;任务&GT; bTask = backup.BackupCurrentDatabaseAsync()
.ContinueWith(_ =&GT;
{
COM pressArchiveAsync();
});
//拆开包装的任务,以产生一个完整的任务
任务T = bTask.Unwrap();
方法 ExtractArchiveAsync()
, BackupCurrentDatabaseAsync()
, RestoreDatabaseAsync()
全部返回工作
。
在这里,第一续返回任务&LT;任务&GT;
。我可以再展开()
这个任务将延续上所产生的(内部)的任务。
第二个版本不会编译。唯一不同的是这里周围的的COM pressArchiveAsync()
。
我试图访问所产生的(内部)工作
检查 Task.Status
。如果我用的是第二种方法,Task.Status正在报告 BackupCurrentDatabaseAsync()
任务的结果。
.ContinueWith(_ =&GT;的COM pressArchiveAsync());
等同于:
.ContinueWith(_ =&GT;
{
返回的COM pressArchiveAsync();
});
注意返回
。
你的第二个code段不编译,因为 ContinueWith
不返回任务&LT;任务&GT;
,而只是一个工作
,并没有什么可以解开。
下面是绑定到 Func键&LT;任务,任务&GT;
(一个函数,它接受一个工作
返回一个工作
)
_ =&GT;
{
返回的COM pressArchiveAsync();
}
但下面其实是必然的动作&LT;任务&GT;
(一个函数,它接受一个工作
,但没有按'T返回任何东西):
_ =&GT;
{
COM pressArchiveAsync();
}
和引用了工作
按的COM pressArchiveAsync
创建的再也没有回来。如果没有一个参考吧,你不能检查状态工作
的。
需要注意的是:
- <一个href="http://msdn.microsoft.com/en-us/library/dd321405.aspx"><$c$c>ContinueWith<TResult>(Func<Task, TResult&GT;) 返回
任务&LT; TResult&GT;
- <一个href="http://msdn.microsoft.com/en-us/library/dd270696.aspx"><$c$c>ContinueWith(Action<Task>)$c$c>返回
工作
。
因此,你的 ContinueWith(Func键&LT;任务,任务&GT;)
返回任务&LT;任务&GT;
,你可以解开,但你的 ContinueWith(动作&LT;任务&GT;)。
只返回一个工作
Can someone please explain the difference between these two statements:
Task<Task> bTask = backup.BackupCurrentDatabaseAsync()
.ContinueWith(_ => CompressArchiveAsync());
//unwrap the tasks to produce one entire task
Task t = bTask.Unwrap();
vs
Task<Task> bTask = backup.BackupCurrentDatabaseAsync()
.ContinueWith(_ =>
{
CompressArchiveAsync();
});
//unwrap the tasks to produce one entire task
Task t = bTask.Unwrap();
The methodsExtractArchiveAsync()
, BackupCurrentDatabaseAsync()
, RestoreDatabaseAsync()
all return a Task
.
Here, the first Continuation returns a Task<Task>
. I can then Unwrap()
this task to put Continuations on the resultant (inner) task.
The second version doesn't compile. The only different here is the braces around the CompressArchiveAsync()
.
I am trying to access the resultant (internal) Task
to check the Task.Status
. If I use the second method, the Task.Status is reporting the result of the BackupCurrentDatabaseAsync()
task.
.ContinueWith(_ => CompressArchiveAsync());
is equivalent to:
.ContinueWith(_ =>
{
return CompressArchiveAsync();
});
Notice the return
.
Your second code snippet doesn't compile because ContinueWith
doesn't return a Task<Task>
, but simply a Task
, and there's nothing to unwrap.
The following is bound to a Func<Task, Task>
(a function that takes a Task
and returns a Task
)
_ =>
{
return CompressArchiveAsync();
}
But the following is actually bound to an Action<Task>
(a function that takes a Task
but doesn't return anything):
_ =>
{
CompressArchiveAsync();
}
And the reference to the Task
created by CompressArchiveAsync
is never returned. Without a reference to it, you can't check the Task
's status.
Note that:
ContinueWith<TResult>(Func<Task, TResult>)
returns aTask<TResult>
ContinueWith(Action<Task>)
returns aTask
.
Therefore your ContinueWith(Func<Task, Task>)
returns a Task<Task>
that you can unwrap, but your ContinueWith(Action<Task>)
simply returns a Task
.
这篇关于我如何能生产任务&LT;任务&GT;解开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!