【问题标题】:C# UI for AWS S3 not updatingAWS S3 的 C# UI 未更新
【发布时间】:2018-06-01 07:20:13
【问题描述】:

我正在用 C# 创建一个下载应用程序,用于从 Amazon AWS S3 存储下载文件。我可以毫无问题地下载文件,但我正在尝试创建一个进度事件。

要创建事件,我在下载函数中使用以下代码:

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

我创建的事件处理程序如下:

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

问题是它只在下载文件时更新,但事件运行得更频繁。当我下载最后一个文件(12MB)时; lable7(事件计数器)从 3 跳到 121,所以我知道它正在运行,但只是没有更新。

我也尝试过“标准”调用,但结果相同。

函数附加代码:

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

【问题讨论】:

  • 你试过没有所有的 DoEvents 调用吗?
  • 是的,我只尝试了 1 个呼叫,然后我尝试了 3 个,接下来我尝试了 0,最后我只在每一行之后都做了一个。我尝试过的任何方式都没有工作。
  • 你使用的是异步版本的下载方式吗?
  • 没有调用是从按钮单击循环中发生的。它不是异步的。我将代码放在原始问题的底部。

标签: c# amazon-web-services amazon-s3 aws-sdk-net


【解决方案1】:

您没有对GetObjectWriteResponseStreamToFile 使用异步调用,因此(您从中调用它的)UI 线程将被阻塞,这意味着它无法更新进度(不管那些DoEvents 电话,通常被认为是邪恶的,你应该避免)。

实际上无法亲自尝试,但我认为您需要这样做。

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

  }
 }

我添加的两个 nulls 用于取消令牌,我无法从 AWS 文档中判断是否允许传递空令牌,如果不允许,请创建一个并传递它。

【讨论】:

  • 谢谢。我只需要更改 WriteResponceStream 行,而且我必须创建一个取消令牌。之后它就没有问题了。
猜你喜欢
  • 2018-06-20
  • 1970-01-01
  • 2018-05-05
  • 2015-07-21
  • 1970-01-01
  • 2021-02-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多