如何在不检查取消挂起 属性 的情况下取消异步调用(立即终止方法调用)
How to cancel an async call without checking the cancellation pending property (Instantly terminating the method call)
我找不到在后台线程上取消异步调用的方法 运行。
假设我有这样的东西:
private async void myMethod()
{
String result = await Task.Run(async () => await myTimeConsumingMethod());
//Do stuff with my result string...
}
这里我在我的后台线程上调用一个异步方法,因为我没有任何循环我不能做这样的事情:
for (i = 0; i < someBigNumber; i++)
{
token.ThrowIfCancellationRequested();
await doSomeWork();
}
相反,我调用了一个异步方法,可能需要超过 10 秒才能完成,我无法检查该方法内的令牌是否已被取消,因为我正在等待异步调用完成。
这就是我想要实现的目标:
private async void myMethod()
{
String result = await Task.Run(async () => await myTimeConsumingMethod());
//Override the HardwareButtons.BackPressed event (I already have a method for that)
//When the await is completed, the String is the result of the async
//call if the method has completed, otherwise the result String
//should be set to null.
}
问题是我不知道在 HardwareButtons.BackPressed
事件中使用什么代码来终止我的异步调用。
我的意思是,我真的想 "terminate its process" 并立即使 Task.Run return 无效。
有办法吗?
这是 myTimeConsumingMethod() 的实现:
public static async Task<String> ToBase64(this StorageFile bitmap)
{
IRandomAccessStream imageStream = await CompressImageAsync(bitmap);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream);
PixelDataProvider pixels = await decoder.GetPixelDataAsync();
byte[] bytes = pixels.DetachPixelData();
return await ToBase64(bytes, (uint)decoder.PixelWidth, (uint)decoder.PixelHeight, decoder.DpiX, decoder.DpiY);
}
这是不可能的。您可以创建一个 Task
,它将在现有操作完成时完成,或者如果取消标记被取消,则将其自身标记为已取消,但这实际上不会停止原始操作,只是允许程序继续执行,尽管实际上操作还没有完成。
有关该主题的更多信息,请参阅 this blog article。
我找不到在后台线程上取消异步调用的方法 运行。 假设我有这样的东西:
private async void myMethod()
{
String result = await Task.Run(async () => await myTimeConsumingMethod());
//Do stuff with my result string...
}
这里我在我的后台线程上调用一个异步方法,因为我没有任何循环我不能做这样的事情:
for (i = 0; i < someBigNumber; i++)
{
token.ThrowIfCancellationRequested();
await doSomeWork();
}
相反,我调用了一个异步方法,可能需要超过 10 秒才能完成,我无法检查该方法内的令牌是否已被取消,因为我正在等待异步调用完成。
这就是我想要实现的目标:
private async void myMethod()
{
String result = await Task.Run(async () => await myTimeConsumingMethod());
//Override the HardwareButtons.BackPressed event (I already have a method for that)
//When the await is completed, the String is the result of the async
//call if the method has completed, otherwise the result String
//should be set to null.
}
问题是我不知道在 HardwareButtons.BackPressed
事件中使用什么代码来终止我的异步调用。
我的意思是,我真的想 "terminate its process" 并立即使 Task.Run return 无效。
有办法吗?
这是 myTimeConsumingMethod() 的实现:
public static async Task<String> ToBase64(this StorageFile bitmap)
{
IRandomAccessStream imageStream = await CompressImageAsync(bitmap);
BitmapDecoder decoder = await BitmapDecoder.CreateAsync(imageStream);
PixelDataProvider pixels = await decoder.GetPixelDataAsync();
byte[] bytes = pixels.DetachPixelData();
return await ToBase64(bytes, (uint)decoder.PixelWidth, (uint)decoder.PixelHeight, decoder.DpiX, decoder.DpiY);
}
这是不可能的。您可以创建一个 Task
,它将在现有操作完成时完成,或者如果取消标记被取消,则将其自身标记为已取消,但这实际上不会停止原始操作,只是允许程序继续执行,尽管实际上操作还没有完成。
有关该主题的更多信息,请参阅 this blog article。