I can't seem to get the below code to result in cancelling the GetAsync()
. It continues to fetch the file. I setup this colours-like diagnostic and can see things happening the way they should (click events raises cancel, the progress frame elsewhere on the screen goes yellow, green and red happen as they should).
cts = new System.Threading.CancellationTokenSource(); //declared earlier...
var token = cts.Token;
BackgroundColor = Color.Red;
try
{
var fileContent = await
App.GraphClient.Me.Drive.Root.ItemWithPath(App.selectedOneDriveFolder + "/" + item.path).Content.Request().GetAsync(token);
// Do nothing with fileContent... this is just for testing
}
catch (Exception ex)
{
await DisplayAlert("Cancelled finally?", ex.Message, ":(");
}
BackgroundColor = Color.Green;
//and then the clicked event handler...
private void CancelDownloadButton_Clicked(object sender, EventArgs e)
{
cts?.Cancel();
ProgressFrame.BackgroundColor = Color.Yellow;
}
I also tried all sorts of different versions of running this (with Task
, with .Run
and/or .Wait(token)
), but to no avail. Any ideas?