首页 > 解决方案 > 取消 Graph 方法 GetAsync(CancellationToken t) 似乎不起作用

问题描述

我似乎无法获得以下代码来取消GetAsync(). 它继续获取文件。我设置了这种类似颜色的诊断程序,并且可以看到事情以应有的方式发生(单击事件引发取消,屏幕上其他地方的进度框架变为黄色、绿色和红色,它们应该发生)。

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;
}

我还尝试了各种不同版本的运行(使用Task、使用.Run和/或.Wait(token)),但无济于事。有任何想法吗?

标签: c#asynchronousxamarin.formsasync-awaitcancellation-token

解决方案


推荐阅读