31

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.

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

Thomas
  • 33,544
  • 126
  • 357
  • 626

1 Answers1

39

Example code with progress from the article

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));
}

If you want to report on each file independently you will have different base type for IProgress:

public Task UploadPicturesAsync(List<Image> imageList, 
     IProgress<int[]> progress)
{
      int totalCount = imageList.Count;
      var progressCount = Enumerable.Repeat(0, totalCount).ToArray(); 
      return Task.WhenAll( imageList.map( (image, index) =>                   
        UploadAndProcessAsync(image, (percent) => { 
          progressCount[index] = percent;
          progress?.Report(progressCount);  
        });              
      ));
}

private async void Start_Button_Click(object sender, RoutedEventArgs e)
{
    int uploads=await UploadPicturesAsync(GenerateTestImages(),
        new Progress<int[]>(percents => ... do something ...));
}
vittore
  • 17,449
  • 6
  • 44
  • 82
  • your code is ok but how could i show multiple progress bar for multiple file upload with status.need guide line. thanks – Thomas Nov 14 '13 at 15:04
  • 1
    @Thomas Your UploadAndProcessAsync itself should follow the same pattern and have `IProgress progress` argument. – vittore Nov 14 '13 at 15:07
  • 4
    not very clear. it will be really helpful if u give me your valuable time to complete the code. suppose imageList has 3 file name and i want to show three progressbar for that. so just guide me how could i add 3 progressbar and also update that progressbar status from UploadPicturesAsync() function. i do not know about what is IProgress and how it works. thanks – Thomas Nov 15 '13 at 06:42