繁体   English   中英

C#用户界面未更新

[英]C# UI not updating

我有一个在.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得到更新?

编辑:我确实看过如何从C#中的另一个线程更新GUI? 并借用了一些想法并实现了“ this.Invoke((MethodInvoker)delegate”),但它仍然无法正常工作。

您必须强制重画。 如果不强行执行,则每项工作完成后都会重新绘制。 在我的应用中,我使用System.Windows.Forms.Application.DoEvents(); 强制重绘。 在此处输入图片说明

我不确定这是否是完整的答案,但它解决了我的问题。 我进行tansferUtility.Upload的调用是阻止调用。 因此,我认为它阻止了UI更新。 我从github下载了AWS开发工具包,并使用了具有transferUtility.BeginUpload调用的.Net 3.5版本。 这不是阻止调用,而是调用BeginInvoke,因此UI没有被阻止并且我的UI得到更新。

暂无
暂无

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

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