如何做到进度使用异步/等待报告 [英] How to do progress reporting using Async/Await

查看:130
本文介绍了如何做到进度使用异步/等待报告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有,我有在C#项目中使用FTP相关的类复制到Web服务器的文件列表。在这里,我想使用异步/等待功能,也想展示在同一时间多个文件上传多个进度条。每一个进度条显示每个文件上传状态。所以引导我如何能做到这一点。

suppose i have a list of files which i have to copy to web server using ftp related classes in c# project. here i want to use Async/Await feature and also want to show multiple progress bar for multiple file uploading at same time. each progress bar indicate each file upload status. so guide me how can i do this.

当我们与背景工人的工作做这样的工作的话,就非常容易,因为后台工作有进步改变事件。因此,如何处理这种带有Async /等待的情况。如果可能的话引导我与样品code。谢谢

when we work with background worker to do this kind of job then it is very easy because background worker has progress change event. so how to handle this kind of situation with Async/Await. if possible guide me with sample code. thanks

推荐答案

举例code从<进度href=\"http://blogs.msdn.com/b/dotnet/archive/2012/06/06/async-in-4-5-enabling-progress-and-cancellation-in-async-apis.aspx\"相对=nofollow>文章

public async Task<int> UploadPicturesAsync(List<Image> imageList, 
     IProgress<int> progress)
{
      int totalCount = imageList.Count;
      int processCount = await Task.Run<int>(() =>
      {
          int tempCount = 0;
          foreach (var image in imageList)
          {
              //await the processing and uploading logic here
              int processed = await UploadAndProcessAsync(image);
              if (progress != null)
              {
                  progress.Report((tempCount * 100 / totalCount));
              }
              tempCount++;
          }
          return tempCount;
      });
      return processCount;
}


private async void Start_Button_Click(object sender, RoutedEventArgs e)
{
    int uploads=await UploadPicturesAsync(GenerateTestImages(),
        new Progress<int>(percent => progressBar1.Value = percent));
}

这篇关于如何做到进度使用异步/等待报告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆