C# async/await Progress event on Task<> object

前端 未结 4 1906
一个人的身影
一个人的身影 2020-12-02 17:22

I\'m completely new to C# 5\'s new async/await keywords and I\'m interested in the best way to implement a progress event.

Now I\'d prefer

4条回答
  •  不思量自难忘°
    2020-12-02 17:42

    Simply put, Task doesn't support progress. However, there's already a conventional way of doing this, using the IProgress interface. The Task-based Asynchronous Pattern basically suggests overloading your async methods (where it makes sense) to allow clients to pass in an IProgess implementation. Your async method would then report progress via that.

    The Windows Runtime (WinRT) API does have progress indicators built-in, in the IAsyncOperationWithProgress and IAsyncActionWithProgress types... so if you're actually writing for WinRT, those are worth looking into - but read the comments below as well.

提交回复
热议问题