当前位置: 首页>>代码示例>>C#>>正文


C# ITokenSource.Cancel方法代码示例

本文整理汇总了C#中ITokenSource.Cancel方法的典型用法代码示例。如果您正苦于以下问题:C# ITokenSource.Cancel方法的具体用法?C# ITokenSource.Cancel怎么用?C# ITokenSource.Cancel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ITokenSource的用法示例。


在下文中一共展示了ITokenSource.Cancel方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Query

        private List<Result> Query(Query query, ITokenSource cancelToken)
        {
            if (query.KeywordComplete && query.HasArguments) {
                // check the cache for a matching result
                var cacheKey = query.Arguments;
                var cachedSuggestions = MemoryCache.Default.Get(cacheKey) as List<string>;

                // if cached result is found, return it.
                if (cachedSuggestions != null) {
                    // convert the list of suggestion strings to a List<Result>
                    if (cachedSuggestions.Any()) {
                        var results = cachedSuggestions.Select(suggestion => new Result
                        {
                            Title = suggestion,
                            Icon = _icon,
                            SubTitle = "Search google for " + suggestion,
                            Launch = query1 =>
                            {
                                Process.Start($"http://google.co.uk/search?q={suggestion}");
                                AppCommands.HideWindow();

                            }
                        }).ToList();
                        return results;
                    }
                    // no suggestions were received from the server
                    return new List<Result>
                    {
                        new Result
                        {
                            Title = "No search suggestions found.",
                            Icon = _icon
                        }
                    };
                }

                // Cache miss, begin the background query to fill the cache
                // create a local cancel token for passing to httpclient..
                var cancellable = new CancellationToken();
                cancellable.Register(() =>
                {
                    cancelToken.Cancel();
                    cancelToken.Dispose();
                });
                var x = GetSuggestionsAsync(query.Arguments, cancellable);
                return new List<Result>
                {
                    new Result
                    {
                        Title = "Retrieving search suggestions...",
                        Icon = _icon
                    }
                };
            }
            // otherwise the query has not been provided yet, running the action will autocomplete the query
            return new List<Result>
            {
                new Result
                {
                    Title = "Search Google",
                    SubTitle = "Search Google with Suggestions",
                    Icon = _icon,
                    Launch = query1 => AppCommands.RewriteQuery(Keyword + ' ')
                }
            };
        }
开发者ID:jameshy,项目名称:else,代码行数:66,代码来源:GoogleSuggest.cs


注:本文中的ITokenSource.Cancel方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。