I am uploading files to S3. But the file upload fails silently without any error. So I have written an event handler to keep track of the upload.
Below is my function for file upload.
I want to wait for event handler till it completes its execution. Please note it is synchronous file upload.
public async Task<ActionResult> UploadBatchDocuments(UploadViewModel model)
{
TransferUtility transfer = new TransferUtility(client);
foreach (var file in model.Documents)
{
TransferUtilityUploadRequest request = new TransferUtilityUploadRequest()
{
BucketName = bucketName,
CannedACL = S3CannedACL.PublicRead,
Key = string.Format(file.FilePath + "/{0}", fileName),
InputStream = file.File.InputStream,
};
await Task.Run(()=>request.UploadProgressEvent += uploadRequest_UploadPartProgressEvent);
await Task.Run(() => transfer.Upload(request));
}
}
Below is my event handler which runs for every file upload,
public async void uploadRequest_UploadPartProgressEvent(object sender, UploadProgressArgs e)
{
if (e.PercentDone == 100)
{
var subs = ((Amazon.S3.Transfer.TransferUtilityUploadRequest)sender).Key.Split('/');
var fileName = subs[subs.Length - 1];
for (int i = 0; i < tempModel.Documents.Count; i++)
{
if (tempModel.Documents[i].File.FileName.Equals(fileName))
{
tempModel.Documents[i].isUploaded = true;
}
}
}
}
My problem is:Control keeps switching between UploadBatchDocuments and uploadRequest_UploadPartProgressEvent(which is an event handler). Required:I want the control to wait in UploadBatchDocuments to wait for the thread to complete in uploadRequest_UploadPartProgressEvent