【发布时间】: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