Support of progress reporting and incremental results in .NET 4.0 “Task Parallel Library”

后端 未结 5 631
有刺的猬
有刺的猬 2020-12-29 07:49

I know that Task Parallel Library is still in Beta and there are likely to be less resources available but from whatever I have read, library gives very special treatment to

5条回答
  •  执笔经年
    2020-12-29 08:42

    There is no built-in support for this like what BackgroundWorker had.

    You can use SynchronizationContext directly; there's an excellent video here: http://www.rocksolidknowledge.com/ScreenCasts.mvc/Watch?video=TasksAndThreadAffinity.wmv

    The author develops two solutions in this video: one using SynchronizationContext and another using Task Continuations. For your problem, continuations will not work, but the SynchronizationContext approach would work fine.

    P.S. If you're creating reusable code, then when you capture SynchronizationContext.Current, you should test for null and (if it is null) use a default-constructed SynchronizationContext instead.

    UPDATE: I've posted code for this on my blog. My solution is actually based on a Task that is scheduled back to the UI thread by a TaskScheduler which uses SynchronizationContext underneath. Unlike the accepted answer, this solution will work for both WPF and Windows Forms.

提交回复
热议问题