异步操作时,需要展示该操作的进度
IProgress<T> Interface
和Progress<T> Class
插一段话:读《C#并发编程经典实例》这本书偶有困惑,深感书中内容过于精炼,或许是作者故意为之,但显然对我这般知识浅薄的人来说,读起来这本书感到晦涩。偶然找到作者的个人博客,看到作者博客中对某一个知识点不同时间点上由浅至深的研究,十分欣喜。作者个人博客地址:https://blog.stephencleary.com/
可查看此书作者博客文章Reporting Progress from Async Tasks了解更多一手知识
文中作者多次提到UI线程,很困惑,因为最近几年基本没在工作中写WPF、WebForm或者WinForm,所以作者说UI线程时很困惑,将其带入WPF、WebForm或者WinForm的使用场景,就好理解了。
不在废话,上文中伪代码例子
static async Task MyMethodAsync(IProgress<double> progress = null)
{
double percentComplete = 0;
while (!done)
{
...
if (progress != null)
progress.Report(percentComplete);
}
}
Progress只有一个Report方法,Report报告进度更改
static async Task CallMyMethodAsync()
{
var progress = new Progress<double>();
progress.ProgressChanged += (sender, args) =>
{
...
};
await MyMethodAsync(progress);
}
IProgress<T>
参数可以为空,这意味着该异步操作不需要报告更改进度。关于如何使用进度,并可以取消该方法的文章,可查看4.5 中的异步: 启用进度和异步 Api 中的取消
Progress<T>.ProgressChanged
不会抛出异常,或者说它抛出的异常会直接抛给上下文context
原文:https://www.cnblogs.com/AlienXu/p/10142085.html