问题描述
我不明白的方式,看看哪些任务正在运行。还有就是Task.Current财产,但如果有运行多个任务?是否有一种方式来获得这种信息?
I can't see a way to see which tasks are running. There is the Task.Current property, but what if there are multiple tasks running? Is there a way to get this kind of information?
另外,有一个内置的方式得到通知,当一个任务开始或完成?
Alternately, is there a built in way to get notified when a task starts or completes?
推荐答案
嘿麦克,在访问的第三方物流尚未完成的任务列表中没有公开的方式。这使得它可用于调试的机制依赖于一个事实,即所有线程将在列举时被冻结,因此它不能在运行时使用。
Hey Mike, there is no public way of accessing the list of pending tasks in TPL. The mechanism that makes it available for the debugger relies on the fact that all threads will be frozen at enumeration time, therefore it can't be used at runtime.
是的,有一个内置的方式得到通知焕任务完成。退房Task.ContinueWith的API。基本上这个API创建目标任务完成时将发射了一个新的任务。
Yes, there's a built in way to get notified whan a task completes. Check out the Task.ContinueWith APIs. Basically this API creates a new task that will fired up when the target task completes.
我假设你想要做一些快速的会计/进度报告在此基础上,如果是那样的话,我建议您致电task.ContinueWith()与TaskContinuationOptions.ExecuteSynchronously标志。当您指定的连续动作将在同一个线程中运行在那里当目标任务完成(如果没有指定这个后续任务排队,像任何其他常规任务)。
I'm assuming you want to do some quick accounting / progress reporting based on this, if that's the case, I'd recommend that you call task.ContinueWith() with the TaskContinuationOptions.ExecuteSynchronously flag. When you specify that the continuation action will be run right there on the same thread when the target task finishes (if you don't specify this the continuation task is queued up like any other regular task).
希望这有助于。
玉山
这篇关于有没有办法知道哪些任务,目前在任务并行库运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!