简体   繁体   English

C#用户界面未更新

[英]C# UI not updating

I have a C# application running under .Net 4.5. 我有一个在.Net 4.5下运行的C#应用​​程序。 It is an application to upload files to AWS S3 bucket using AWS .Net SDK. 这是一个使用AWS .Net SDK将文件上传到AWS S3存储桶的应用程序。 All the code in under the Form class. Form类下的所有代码。 Evrything works fine, except my progress bar does not update per the progress events fired. Evrything可以正常工作,除了我的进度条不会根据触发的进度事件进行更新。 Here is the relevant code. 这是相关的代码。 Note that this is a Form application with several UI components. 请注意,这是具有多个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);
    }
  }
}

The 'UploadProgressEvent' is where my progress bar is being updated and also a text label. “ UploadProgressEvent”是我的进度条正在更新的地方,也是文本标签。 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. 我知道事件肯定会被触发,因为Logger.logentry方法会在文本文件中按需记录一行,并且在调试器中也会注意到该行。 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%. 上传完成后,事件似乎已被UI占用,进度条会立即更新为100%。

I am it has to do something with the threading but how do I make sure the ui gets updated? 我必须对线程做些什么,但是如何确保ui得到更新?

EDIT: I did look at the How to update the GUI from another thread in C#? 编辑:我确实看过如何从C#中的另一个线程更新GUI? and borrowed some ideas and implemented "this.Invoke((MethodInvoker)delegate" but it still does not work. 并借用了一些想法并实现了“ this.Invoke((MethodInvoker)delegate”),但它仍然无法正常工作。

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(); 在我的应用中,我使用System.Windows.Forms.Application.DoEvents(); to do force the redraw. 强制重绘。 在此处输入图片说明

I am not sure if this is complete answer, but it solved my issue. 我不确定这是否是完整的答案,但它解决了我的问题。 The call I make tansferUtility.Upload is a blocking call. 我进行tansferUtility.Upload的调用是阻止调用。 So I think it was blocking the UI updates. 因此,我认为它阻止了UI更新。 I downloaded AWS SDK from github and used the .Net 3.5 version which has the transferUtility.BeginUpload call. 我从github下载了AWS开发工具包,并使用了具有transferUtility.BeginUpload调用的.Net 3.5版本。 This is not blocking call, calls BeginInvoke and hence the UI is not blocked and my UI gets updated. 这不是阻止调用,而是调用BeginInvoke,因此UI没有被阻止并且我的UI得到更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM