2

After I got my uploadoperation to complete successfully, I am now trying to get some progress UI. right now here's my code including the progress callback and the handler for that callback.

public async Task<UploadOperation> BackgroundUploadVideoAsync(IStorageFile file, string gfyName)
{
    BackgroundUploader uploader = new BackgroundUploader();

    //… Create upload ...


    var partOperation = await uploader.CreateUploadAsync(new Uri("https://filedrop.gfycat.com/"), contentParts, "form-data", Guid.NewGuid().ToString() );
    var progress = new Progress<UploadOperation>(Shell.Current.HandleUpload);
    try
    {
        await partOperation.StartAsync().AsTask(progress);
        return partOperation;
        //var response = await operation.StartAsync().AsTask();
    }
    catch(Exception e)
    {

        var status = BackgroundTransferError.GetStatus(e.HResult);
        //await new MessageDialog($"An error occurred: {status}").ShowAsync();

    }
    return partOperation;
}

public async void HandleUpload(UploadOperation operation)
{
    await Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
    {
        var percent = operation.Progress.BytesSent / operation.Progress.TotalBytesToSend;
        progressBar.Value = Math.Truncate((double)percent);
        });
    }
}

The problem is when HandleUpload gets called, the 'bytessent' and 'bytestosend' are equal immediately, and my progress bar is set to 100% and waits there until the task is complete. I've never dealt with progress callbacks before so I might be doing it completely wrong.

aweyeahdawg
  • 886
  • 9
  • 19
  • Note that I think your `percent` variable will only ever have a value of between 0.0 and 1.0 assigned to it, and Math.Truncate will round it to 0 if it's less than 1.0, which isn't going to make for a very useful progress bar. – Ian Sep 09 '18 at 00:14
  • Oh, good call. I could just get rid of the Math.Truncate and cast it. – aweyeahdawg Sep 09 '18 at 01:36
  • @aweyeahdawg Have you solved your issue? – Xie Steven Sep 19 '18 at 01:49
  • No, but it's working and I'm just not going to show progress. – aweyeahdawg Sep 20 '18 at 15:48
  • @aweyeahdawg I used the official [BackgroundTransfer](https://github.com/Microsoft/Windows-universal-samples/blob/master/Samples/BackgroundTransfer/cs/BackgroundTransfer/Scenario2_Upload.xaml.cs#L286) sample to test the progress callback, it worked well. Please use it to test your upload operation to see if it works for you. – Xie Steven Sep 27 '18 at 07:46

0 Answers0