[英]How to gracefully handle TaskCancelledException while using SearchBox in Windows 8.1
I am using a searchbox to which I will load a list of names. 我正在使用一个搜索框,我将加载一个名单列表。
My code behind 我的代码背后
private async void SearchBox_SuggestionsRequested(SearchBox sender, SearchBoxSuggestionsRequestedEventArgs args)
{
if (string.IsNullOrEmpty(args.QueryText))
{
return;
}
var collection = args.Request.SearchSuggestionCollection;
if(oldquery != args.QueryText && args.Request.IsCanceled == false)
{
var deferral = args.Request.GetDeferral();
try
{
oldquery = args.QueryText;
var listOfBanks = await addFIPageViewModel.GetBanksOnQuery();
foreach (Institution eachBank in listOfBanks)
{
collection.AppendQuerySuggestion(eachBank.Name);
}
}
//JUST Logging and ignoring. Can I handle it in a better way
catch(Exception e)
{
Debug.WriteLine(e.StackTrace);
}
finally
{
deferral.Complete();
}
}
}
An exception of type 'System.Threading.Tasks.TaskCanceledException' A task was canceled. 类型'System.Threading.Tasks.TaskCanceledException'的例外已取消任务。 is occuring in the line
正在排队
var listOfBanks = await addFIPageViewModel.GetBanksOnQuery();
which I am simply ignoring as you see. 如你所见,我根本无视。
Is there a better way to handle this? 有没有更好的方法来处理这个?
I was unable to identify the root cause of this problem. 我无法确定此问题的根本原因。 Could someone guide if this is the right way to call an async method inside SearchSuggestionRequested.
如果这是在SearchSuggestionRequested中调用异步方法的正确方法,有人可以指导。
Your code looks fine to me. 你的代码看起来很好。 I'd catch the TaskCanceledException explicitly though to make sure I do not ignore other exceptions unintentionally.
我会明确捕获TaskCanceledException,以确保我不会无意中忽略其他异常。
try
{
// like above
}
catch(TaskCanceledException e)
{
Debug.WriteLine("Task cancelled: " + e.Message);
}
finally
{
deferral.Complete();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.