简体   繁体   English

如何在Windows 8.1中使用SearchBox时优雅地处理TaskCancelledException

[英]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.

相关问题 如何使用异步方法加载结果,在Windows 8.1中使用SearchBox - How to use SearchBox in Windows 8.1 by loading the results using a async method Windows 8.1的SearchBox结果建议 - SearchBox result suggestion Windows 8.1 如何在Windows Phone 8.1中处理“ HttpClient”的事件 - How to handle Events for “HttpClient” in Windows Phone 8.1 如何在Windows服务的后台线程中正常处理此异常? - How do I gracefully handle this exception in a background thread in a Windows service? 如何在Windows Phone 8.1中处理预填充的数据库 - How to handle prepopulated database in Windows Phone 8.1 如何在Windows Store App中使用SearchBox搜索内容? - How to search content using SearchBox in Windows Store App? 如何在Android Http postasync中处理TaskCancelledException? 我不断收到未处理的异常和应用程序崩溃 - How to handle TaskCancelledException in Android Http postasync ? I keep getting unhandled exception and app crash 赢取8.1 SearchBox建议请求 - Win 8.1 SearchBox SuggestionsRequested 由于超时,如何修复 TaskCancelledException? - How to fix TaskCancelledException due to timeout? 如何使用Windows Phone 8.1应用程序处理所有文件类型 - How to handle all file types with my Windows Phone 8.1 app
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM