如何一个System.Threading.Tasks.Task已经完成得到通知 [英] How to get notification that a System.Threading.Tasks.Task has completed

查看:1399
本文介绍了如何一个System.Threading.Tasks.Task已经完成得到通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是.NET 4中发现的新System.Threading.Tasks功能,当前正在替换一些家庭烘焙的任务功能与新的实现。

I am currently replacing some home baked task functionality with a new implementation using the new System.Threading.Tasks functionality found in .net 4.

我有一个小问题,虽然,虽然我能想到的一些解决方案,我想了一些建议通常是做到这一点的最好办法,如果我缺少一个窍门的地方。

I have a slight issue though, and although I can think of some solutions I would like some advice on which is generally the best way to do it, and if I am missing a trick somewhere.

我需要的是一个任意进程能够启动一个任务,但再进行,而不是等待任务完成。不是一个问题,但是当我再需要做一些与任务我不太清楚这样做的最佳方式的结果。

What I need is for an arbitrary process to be able to start a Task but then carry on and not wait for the Task to finish. Not a problem, but when I then need to do something with the result of a task i'm not quite sure the best way of doing it.

所有我见过的例子使用或者等待()的任务,直到它完成或引用的任务的结果参数。这两个会阻止赖以起家的任务,这是我不希望的线程。

All the examples I have seen use either Wait() on the task until it completes or references the Result parameter on the task. Both of these will block the thread which started the Task, which I don't want.

一些解决方案,我已经想到了:

Some solutions I have thought of:

  1. 创建一个新的线程,并开始该任务,然后使用等待()或。结果,以阻止新的线程同步的结果返回给调用者不知何故,可能与轮询IsCompleted参数的任务。

  1. Create a new thread and start the task on that, then use Wait() or .Result to block the new thread and sync the result back to the caller somehow, possibly with polling to the tasks IsCompleted parameter.

有一个'通知已完成的任务,我可以我想运行,然后提出了一个静态的事件或某事完成任务后开始。

Have a 'Notify Completed' task which I can start after completion of the task I want to run which then raises a static event or something.

传递一个委托到任务的输入和调用以通知的任务就完成了。

Pass a delegate into the input of the task and call that to notify that the task is finished.

我能想到或正反向所有的人,但我特别不喜欢有明确创建一个新的线程,开始对任务的想法时使用Task类中的目标之一第一个地方是抽象远离直接线程使用。

I can think or pros and cons to all of them, but I especially don't like the idea of having to explicitly create a new thread to start the task on when the one of the aims of using the Task class in the first place is to abstract away from direct Thread usage.

有关的最佳方式有什么想法?我失去了一些东西简单?一会已完成的事件太多,要求:)? (肯定有一个很好的理由,为什么没有一个!)

Any thoughts about the best way? Am I missing something simple? Would a 'Completed' event be too much to ask for :)? (Sure there is a good reason why there isn't one!)

推荐答案

我的犯罪嫌疑人的您正在寻找的 Task.ContinueWith (或<一href="http://msdn.microsoft.com/en-us/library/dd321274.aspx"><$c$c>Task<T>.ContinueWith).这些基本上说,当你完成这个任务,执行此操作。不过,也有可以指定要采取更多的控制权的各种选项。

I suspect you're looking for Task.ContinueWith (or Task<T>.ContinueWith). These basically say, "When you've finished this task, execute this action." However, there are various options you can specify to take more control over it.

MSDN进入了很多更详细的关于这如何:链接多个任务,延续延续任务

MSDN goes into a lot more detail on this in "How to: Chain Multiple Tasks With Continuations" and "Continuation Tasks".

这篇关于如何一个System.Threading.Tasks.Task已经完成得到通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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