Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Windows 8.1中使用SearchBox时如何优雅地处理TaskCancelledException_C#_Winrt Xaml_Windows 8.1_Winrt Async_Win Universal App - Fatal编程技术网

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

C# 在Windows 8.1中使用SearchBox时如何优雅地处理TaskCancelledException,c#,winrt-xaml,windows-8.1,winrt-async,win-universal-app,C#,Winrt Xaml,Windows 8.1,Winrt Async,Win Universal App,我正在使用一个搜索框,我将向其中加载一个姓名列表 我的代码隐藏 private async void SearchBox_SuggestionsRequested(SearchBox sender, SearchBoxSuggestionsRequestedEventArgs args) { if (string.IsNullOrEmpty(args.QueryText)) { return; } v

我正在使用一个搜索框,我将向其中加载一个姓名列表

我的代码隐藏

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

        }
    }
类型为“System.Threading.Tasks.TaskCanceledException”的异常 一项任务被取消。正在排队等候

var listOfBanks = await addFIPageViewModel.GetBanksOnQuery();
正如你所看到的,我只是忽略了这一点

有没有更好的方法来处理这个问题


我无法确定这个问题的根本原因。有没有人能告诉我这是否是在SearchSuggestionRequested中调用异步方法的正确方法。

我觉得你的代码很好。不过,我会显式捕获TaskCanceledException,以确保不会无意中忽略其他异常

try
{
    // like above 
}
catch(TaskCanceledException e)
{
    Debug.WriteLine("Task cancelled: " + e.Message);
}
finally
{
    deferral.Complete();
}