Task with event notification - .net 4

后端 未结 2 1624
悲&欢浪女
悲&欢浪女 2021-01-06 02:38

Yesterday on SO, i saw a thread asking for a code which some what does this way. I mean, you (manager thread) start number of tasks using TPL APIs and once they are complete

相关标签:
2条回答
  • 2021-01-06 02:57

    Alternatively you can try using async / await pattern. If you await a Task that means the compiler will automatically generate the ContinueWith for you.

    If you use this pattern you can write normal async methods which will itself generate State machine.

    Check my post

    http://www.codeproject.com/KB/cs/async.aspx

    Rx on the other hand can represent your data flow, I mean you can hook a sequence of jobs and subscribe your observable on an Observer.

    http://msdn.microsoft.com/en-us/data/gg577609

    0 讨论(0)
  • 2021-01-06 02:59

    There's no need to do this yourself at all - use continuations with Task.ContinueWith or Task<T>.ContinueWith. That basically lets you say what to do when a task completes - including executing different code on failure, cancellation, and success.

    It also returns a task, so you can then continue when that's completed etc.

    Oh, and you can give it a TaskScheduler, so that from a UI thread you can say, "When this background task finishes, execute the given delegate on the UI thread" or similar things.

    This is the approach that C# 5 async methods are built on.

    0 讨论(0)
提交回复
热议问题