C#UI没有更新

时间:2016-03-26 19:35:08

标签: c# amazon-s3

我有一个在.Net 4.5下运行的C#应用​​程序。它是使用AWS .Net SDK将文件上载到AWS S3存储桶的应用程序。 Form类下的所有代码。 Evrything工作正常,但我的进度条不会根据触发的进度事件更新。这是相关的代码。请注意,这是一个包含多个UI组件的Form应用程序。

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

' UploadProgressEvent'是我的进度条正在更新的位置,也是文本标签。我确定事件被解雇了,因为Logger.logentry方法在文本文件中记录了一行,并且在调试器中也注意到了。但是,进度条不会更新。上传完成后,事件似乎被UI消耗,进度条匆忙更新为100%。

我必须对线程做些什么,但我如何确保ui得到更新?

编辑:我确实看了How to update the GUI from another thread in C#?并借用了一些想法并实施了#34; this.Invoke((MethodInvoker)委托"但它仍然不起作用。

2 个答案:

答案 0 :(得分:0)

你必须强制重绘。如果你不强迫它会在每个工作完成后重绘。 在我的应用程序中我使用 System.Windows.Forms.Application.DoEvents();强制重绘。enter image description here

答案 1 :(得分:0)

我不确定这是否是完整答案,但它解决了我的问题。我做的调用tansferUtility.Upload是一个阻塞调用。所以我认为它阻止了UI更新。我从github下载了AWS SDK,并使用了具有transferUtility.BeginUpload调用的.Net 3.5版本。这不是阻塞调用,调用BeginInvoke,因此UI不会被阻止,我的UI也会更新。