简体   繁体   English

AWS S3的C#UI无法更新

[英]C# UI for AWS S3 not updating

I am creating a download application in C# that downloads files from Amazon AWS S3 storage. 我正在用C#创建一个下载应用程序,该程序从Amazon AWS S3存储中下载文件。 I am able to download the file without issue, but I am trying to create a progress event. 我可以下载文件而不会出现问题,但是我正在尝试创建进度事件。

To create the event I am using the following code within the download function: 为了创建事件,我正在下载功能中使用以下代码:

Application.DoEvents();
response2.WriteObjectProgressEvent += displayProgress;
Application.DoEvents();

The event handler I created is as follows: 我创建的事件处理程序如下:

private void displayProgress(object sender, WriteObjectProgressArgs args)
{
    // Counter for Event runs
    label7.BeginInvoke(new Action(() => label7.Text = (Convert.ToInt32(label7.Text)+1).ToString()));

    Application.DoEvents(); 
    // transferred bytes
    label4.BeginInvoke(new Action(() => label4.Text = args.TransferredBytes.ToString()));

    Application.DoEvents();
    // progress bar
    progressBar1.BeginInvoke(new Action(() => progressBar1.Value = args.PercentDone));

    Application.DoEvents();
}

The issue is that it only updates when a file it downloaded, but the event runs more often. 问题在于它仅在下载文件时更新,但事件运行频率更高。 When I download the last file (12MB); 当我下载最后一个文件(12MB)时; lable7 (event counter) jumps from 3 to 121, so I know it is running, but just not updating. lable7(事件计数器)从3跳到121,所以我知道它正在运行,但没有更新。

I have also tried just a 'standard' Invoke, but I had the same result. 我还尝试了“标准”调用,但结果却相同。

Additional Code of the function: 该功能的附加代码:

AmazonS3Config S3Config = new AmazonS3Config
{
    ServiceURL = "https://s3.amazonaws.com"
};

var s3Client = new AmazonS3Client(stuff, stuff, S3Config);

ListBucketsResponse response = s3Client.ListBuckets();

GetObjectRequest request = new GetObjectRequest();
request.BucketName = "dr-test";
request.Key = locationoffile[currentdownload];


GetObjectResponse response2 = s3Client.GetObject(request);

response2.WriteObjectProgressEvent += displayProgress;


string pathlocation = Path.GetDirectoryName(Directory.GetCurrentDirectory()) + "\\" + Instrument[currentdownload] + "\\" + NewFileName[currentdownload];

response2.WriteResponseStreamToFile(pathlocation);

You're not using the asynchronous call for GetObject or WriteResponseStreamToFile , so the UI thread (that you're calling it from) is going to be blocked, which means that it can't update the progress (regardless of those DoEvents calls, which are generally considered evil and you should avoid). 您没有使用对GetObjectWriteResponseStreamToFile的异步调用,因此UI线程(您从中进行调用)将被阻止,这意味着它无法更新进度(无论那些DoEvents调用如何),通常被认为是邪恶的,您应该避免)。

Without actually being able to try it myself, here's what I think you need to do. 在我自己无法尝试的情况下,这就是我认为您需要做的。

private async void Button_Click(object sender, EventArgs e)
{
   foreach(...download....in files to download){

        AmazonS3Config S3Config = new AmazonS3Config
        {
            ServiceURL = "https://s3.amazonaws.com"
        };

        var s3Client = new AmazonS3Client(stuff, stuff, S3Config);

        ListBucketsResponse response = s3Client.ListBuckets();

        GetObjectRequest request = new GetObjectRequest();
        request.BucketName = "dr-test";
        request.Key = locationoffile[currentdownload];



        GetObjectResponse response2 = await s3Client.GetObjectAsync(request, null);

        response2.WriteObjectProgressEvent += displayProgress;



        string pathlocation = Path.GetDirectoryName(Directory.GetCurrentDirectory()) + "\\" + Instrument[currentdownload] + "\\" + NewFileName[currentdownload];

        await response2.WriteResponseStreamToFileAsync(pathlocation, null);

  }
 }

The two null s that I added are for cancellation tokens, I can't tell from the AWS docs if it's allowed to pass a null token, if not please create one and pass it in. 我添加的两个null代表取消令牌,我无法从AWS文档中得知是否允许传递null令牌,如果不允许,请创建一个并将其传递。

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

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