0

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

Fathima
  • 41
  • 6
  • So it doesnt complete? – TheGeneral Feb 12 '18 at 08:40
  • Possible duplicate of [Is it possible to await an event instead of another async method?](https://stackoverflow.com/questions/12858501/is-it-possible-to-await-an-event-instead-of-another-async-method) – John Wu Feb 12 '18 at 08:43
  • @MichaelRandall, In many cases it wont wait for event handler to complete. – Fathima Feb 12 '18 at 09:30
  • I have lot of confusion here, as I am not using threading concepts. How can I ask my control to wait in UploadBatchDocuments while my event handler is running – Fathima Feb 12 '18 at 10:48

0 Answers0