.NET 4.5で動作するC#アプリケーションがあります。 AWS .Net SDKを使用してファイルをAWS S3バケットにアップロードするアプリケーションです。 Formクラスの下にあるすべてのコード。 Evrythingは正常に機能しますが、プログレスバーは起動された進行イベントごとに更新されません。ここに関連コードがあります。これは、いくつかのUIコンポーネントを持つフォームアプリケーションです。C#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メソッドは、テキストファイルに行を記録し、デバッガでも気付くため、イベントが発生していることを確認しています。ただし、進行状況バーは更新されません。アップロードが完了すると、イベントがUIによって消費されるように見えます。進捗バーは急いで100%に更新されます。
私はスレッディングで何かをしなければならないのですが、どうすればuiが更新されるのを確認できますか?
EDIT:私はHow to update the GUI from another thread in C#?を見ていたし、いくつかのアイデアと実装「this.Invoke((MethodInvoker)デリゲート」を借りたが、それはまだ動作しません
[C#の別のスレッドからGUIを更新するにはどうすればいいですか?](http://stackoverflow.com/questions/661561/how-to-update-the-gui-from-another-thread-in- c) – ganchito55