我如何能生产任务<任务>解开 [英] How can I produce a Task<Task> to Unwrap

查看:115
本文介绍了我如何能生产任务<任务>解开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释一下这两个语句之间的区别:

 任务<任务> bTask = backup.BackupCurrentDatabaseAsync()
    .ContinueWith(_ =>的COM pressArchiveAsync());
//拆开包装的任务,以产生一个完整的任务
任务T = bTask.Unwrap();
 

VS

 任务<任务> bTask = backup.BackupCurrentDatabaseAsync()
    .ContinueWith(_ =>
{
    COM pressArchiveAsync();
});
//拆开包装的任务,以产生一个完整的任务
任务T = bTask.Unwrap();
 

方法 ExtractArchiveAsync() BackupCurrentDatabaseAsync() RestoreDatabaseAsync()全部返回工作

在这里,第一续返回任务<任务> 。我可以再展开()这个任务将延续上所产生的(内部)的任务。

第二个版本不会编译。唯一不同的是这里周围的的COM pressArchiveAsync()

括号

我试图访​​问所产生的(内部)工作检查 Task.Status 。如果我用的是第二种方法,Task.Status正在报告 BackupCurrentDatabaseAsync()任务的结果。

解决方案

  .ContinueWith(_ =>的COM pressArchiveAsync());
 

等同于:

  .ContinueWith(_ =>
{
    返回的COM pressArchiveAsync();
});
 

注意返回

你的第二个code段不编译,因为 ContinueWith 不返回任务<任务> ,而只是一个工作,并没有什么可以解开。

下面是绑定​​到 Func键<任务,任务> (一个函数,它接受一个工作返回一个工作

  _ =>
{
    返回的COM pressArchiveAsync();
}
 

但下面其实是必然的动作<任务> (一个函数,它接受一个工作,但没有按'T返回任何东西):

  _ =>
{
    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>)返回工作

因此​​,你的 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:

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屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆