Task with event notification - .net 4

后端 未结 2 1625
悲&欢浪女
悲&欢浪女 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:59

    There's no need to do this yourself at all - use continuations with Task.ContinueWith or Task.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.

提交回复
热议问题