Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#UI未更新_C#_Amazon S3 - Fatal编程技术网

C#UI未更新

C#UI未更新,c#,amazon-s3,C#,Amazon S3,我有一个C#应用程序在.NET4.5下运行。它是一个使用AWS.Net SDK将文件上载到AWS S3 bucket的应用程序。表单类下的所有代码。除了我的进度条不会根据触发的进度事件进行更新之外,所有东西都可以正常工作。这是相关代码。请注意,这是一个具有多个UI组件的表单应用程序 public partial class Form1 : Form { /* lots of GUI stuff deleted. */ private void upload_file_2_s3(string

我有一个C#应用程序在.NET4.5下运行。它是一个使用AWS.Net SDK将文件上载到AWS S3 bucket的应用程序。表单类下的所有代码。除了我的进度条不会根据触发的进度事件进行更新之外,所有东西都可以正常工作。这是相关代码。请注意,这是一个具有多个UI组件的表单应用程序

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方法会在文本文件中记录一行,并且在调试器中也会注意到这一点。但是,进度条不会得到更新。一旦上传完成,事件似乎会被用户界面消耗掉,进度条会立即更新到100%

我很抱歉,它必须与线程有关,但如何确保ui得到更新


编辑:我确实看过并借鉴了一些想法,实现了“this.Invoke((MethodInvoker)delegate”,但它仍然不起作用。

您必须强制重画。如果不强制重画,它将在每项工作完成后重画。 在我的应用程序中,我使用
System.Windows.Forms.Application.DoEvents();强制重新绘制。

我不确定这是否是完整的答案,但它解决了我的问题。我进行的调用TansPerutility.Upload是一个阻止调用。因此我认为它阻止了UI更新。我从github下载了AWS SDK,并使用了.Net 3.5版本,其中包含transferUtility.BeginUpload调用。这不是阻止调用,而是调用BeginInvoke,因此UI不会被阻止,我的UI也会被更新。

可能重复感谢您的帮助,我尝试了此方法,但它不起作用。请参阅下面的答案,了解我是如何解决此问题的。