等待任务延续中的先行词 [英] Waiting for antecedent in Task continuation

查看:111
本文介绍了等待任务延续中的先行词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个非常愉快的早晨。



我想检查一下我没有错过什么。我正在查看许多代码,这些代码遵循关闭任务的模式,然后在继续中有一个调用等待继续执行的任务( antecedent ,是一个好的M $公民)。



这对我来说似乎很疯狂,因为延续应该只在第一个任务完成后运行,而且我们都看到了整个时间的疯狂代码,但是那就是:



http://msdn.microsoft.com/en-us/library/dd270696(v = vs.110).aspx [ ^ ]



这个位于底部:

A very good morning to all.

I want to check I'm not missing something. I'm looking at a lot of code which is following the pattern of kicking a task off, then in a continuation there is a call to wait on the task being continued from (the antecedent, to be a good M$ citizen).

This seems bonkers to me as the continuation should only run once the first task completes, and we all see bonkers code the whole time, but then there's this:

http://msdn.microsoft.com/en-us/library/dd270696(v=vs.110).aspx[^]

which has this bit at the bottom:

Task.WaitAll( new Task[] {dTask, dTask2} );





..等待初始任务及其继续完成。现在,如果这纯粹是为了阻止调用线程,直到进程完成,肯定只有第二个任务需要等待。



这可能是捕获异常或由于其他原因。那里没有真正的问题,但欢迎意见,你怎么看?谢谢。



..which waits for the initial task and its continuation to finish. Now, if this is purely to block the calling thread until the process is complete surely only the second task needs waiting on.

This may be good practice to catch exceptions or for some other reason. No real question there, but opinions welcome, what do you think? Thanks.

推荐答案

公民)。



这对我来说似乎有点疯狂,因为延续应该只运行一次第一个任务完成了,我们都看到了整个时间的疯狂代码,但是就是这样:



http://msdn.microsoft.com/en-us/library/dd270696(v=vs.110).aspx [ ^ ]



这个位于底部:

citizen).

This seems bonkers to me as the continuation should only run once the first task completes, and we all see bonkers code the whole time, but then there's this:

http://msdn.microsoft.com/en-us/library/dd270696(v=vs.110).aspx[^]

which has this bit at the bottom:
Task.WaitAll( new Task[] {dTask, dTask2} );





..等待初始任务及其继续完成。现在,如果这纯粹是为了阻止调用线程,直到进程完成,肯定只有第二个任务需要等待。



这可能是捕获异常或由于其他原因。那里没有真正的问题,但欢迎意见,你怎么看?谢谢。



..which waits for the initial task and its continuation to finish. Now, if this is purely to block the calling thread until the process is complete surely only the second task needs waiting on.

This may be good practice to catch exceptions or for some other reason. No real question there, but opinions welcome, what do you think? Thanks.


看起来很奇怪,虽然代码下面的解释可能会有所启发:



因为控制台应用程序可以在继续执行任务之前终止,调用WaitAll方法以确保原始任务及其继续在示例结束之前完成执行。



换句话说,等待 dTask dTask2 可以防止dTask完成且dTask2尚未开始执行的情况,作为控制台应用程序将无法等待 Task.Factory.StartNew()返回的dTask2



值得记住的是dTask2本身并不执行,而是启动异步任务。在这方面,dTask2可以被视为生成内部任务的包装器,并在此内部任务上调用Task.Run()。等待dTask2不起作用,因为它在产生内部任务后立即返回。应该可以解开dTask2并等待它的内部任务,尽管这可能不是这里的意图 - 因为任务是使用 Task.Factory 而不是更直接地使用构造函数创建的。这是一个有趣的博客,围绕这个主题:

Task.Run vs Task.Factory.StartNew,作者:Stephen Toub [ ^ ]



其中一些复杂性可能源于dTask2异步。通过在序列连续中运行 dTask 和dTask2(而不是并行延续),使用流利的方式来探索是否可以避免这种情况将会很有趣接口模式,链接 .ContinueWith(dTask2 => ... Task.Factory.StartNew()
It does look odd, though the explanation below the code may shed some light:

"Because a console application may terminate before the continuation task executes, the WaitAll method is called to ensure that both the original task and its continuation finish executing before the example ends."

In other words, waiting on both dTask and dTask2 is there to guard against a situation in which dTask completes and dTask2 has not started execution yet, as the console app will not be able to wait on dTask2 returned by Task.Factory.StartNew()

It might be worth bearing in mind that dTask2 does not execute in itself but rather kicks-off an asynchronous task. In this respect, dTask2 can be viewed as a wrapper which spawns an inner task, and calls Task.Run() on this inner task. Waiting on dTask2 would not work, as it immediately returns after spawning off the inner task. It should be possible to unwrap dTask2 and wait on its inner task, though this is probably not what is intended here - being as tasks were created using Task.Factory rather than more directly using the constructor. Here is an interesting blog which is centred around this topic:
Task.Run vs Task.Factory.StartNew, by Stephen Toub[^]

Some of this complexity, perhaps, stems from dTask2 being asynchronous. It would be interesting to explore whether this could be avoided by running dTask and dTask2 in sequence continuation (as opposed to parallel continuation) using the fluent interface pattern, chaining .ContinueWith(dTask2 => ... onto Task.Factory.StartNew()


这篇关于等待任务延续中的先行词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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