0

I have a C# application running under .Net 4.5. It is an application to upload files to AWS S3 bucket using AWS .Net SDK. All the code in under the Form class. Evrything works fine, except my progress bar does not update per the progress events fired. Here is the relevant code. Note that this is a Form application with several UI components.

public partial class Form1 : Form
{

/*

lots of GUI stuff deleted.

*/
private void upload_file_2_s3(string upload_bucket, string temp_file_name)
{
    // To get the file name with extension to be saved in bucket. 
    string strFileName = new System.IO.FileInfo(temp_file_name).Name; // file name with no path infront of it.

    IAmazonS3 s3Client = new AmazonS3Client(cre, reg);

    Logger.logEntry("AWS client openend for :" + temp_file_name);
    var transferConfig = new TransferUtilityConfig
    {
        ConcurrentServiceRequests = 5,
        MinSizeBeforePartUpload = 20 * 1024 * 1024 // 20MB
    };

    try
    {
        using (var transferUtility = new TransferUtility(s3Client, transferConfig))
        {
            var transferRequest = new TransferUtilityUploadRequest
            {
                Key = strFileName,
                FilePath = temp_file_name,
                BucketName = upload_bucket,
                PartSize = (200 * 1024 * 1024), // 20MB chunks
                AutoCloseStream = true
            };

            transferRequest.UploadProgressEvent += (source, arg) =>
            {
                var filename = strFileName;
                string progress = String.Format(
                        "{0:0.00}/{1:0.00} MB uploaded. {2}% complete.",
                        utils.convertBytes2MB(arg.TransferredBytes), utils.convertBytes2MB(arg.TotalBytes), arg.PercentDone);
                Logger.logEntry(filename + "   " + progress);
                this.Invoke((MethodInvoker)delegate
                {
                    progressBar1.Value = arg.PercentDone; // runs on UI thread
                    label_File_Progress.Text = progress;
                    if(arg.PercentDone == 100 && filename.EndsWith(".zip"))
                    {
                        Logger.logEntry("uploadCompleted file :" + filename);
                        //MessageBox.Show("Congratulations!! Your files were successfully uploaded");
                    }
               });

            };
            transferUtility.Upload(transferRequest);
            //var res = transferUtility.BeginUpload(transferRequest, new AsyncCallback(uploadComplete), strFileName);
            //transferUtility.EndUpload(res);
            Logger.logEntry("Upload completed");
        }
    }
    catch (Exception ex)
    {
        Logger.logEntry("Exception during upload :" + ex);
    }
  }
}

The 'UploadProgressEvent' is where my progress bar is being updated and also a text label. I know for sure the event is getting fired because the Logger.logentry method logs a line in the text file as it should, and also noticed in the debugger. However the progress bar does not get updated. Once the upload is complete, the events seems to get consumed by the UI, the progress bar updates in a hurry to 100%.

I am it has to do something with the threading but how do I make sure the ui gets updated?

EDIT: I did look at the How to update the GUI from another thread in C#? and borrowed some ideas and implemented "this.Invoke((MethodInvoker)delegate" but it still does not work.

Community
  • 1
  • 1
user618886
  • 325
  • 6
  • 16
  • Possible duplicate of [How to update the GUI from another thread in C#?](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in-c) – ganchito55 Mar 26 '16 at 19:44

2 Answers2

0

You must force to redraw. If you do not force it will redraw once every work is finished. In my app I use System.Windows.Forms.Application.DoEvents(); to do force the redraw.enter image description here

0

I am not sure if this is complete answer, but it solved my issue. The call I make tansferUtility.Upload is a blocking call. So I think it was blocking the UI updates. I downloaded AWS SDK from github and used the .Net 3.5 version which has the transferUtility.BeginUpload call. This is not blocking call, calls BeginInvoke and hence the UI is not blocked and my UI gets updated.

user618886
  • 325
  • 6
  • 16