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


C# CancellationToken.WithTimeout方法代码示例

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


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

示例1: WaitForAsync

        private static async Task<bool> WaitForAsync(
            [NotNull] ServiceController serviceController,
            ServiceControllerStatus status,
            CancellationToken token = default(CancellationToken))
        {
            if (serviceController == null) throw new ArgumentNullException("serviceController");

            // TODO Remove constant timeout
            using (ITokenSource tokenSource = token.WithTimeout(TimeSpan.FromMinutes(1)))
            {
                token = tokenSource.Token;

                serviceController.Refresh();
                while (serviceController.Status != status)
                {
                    // ReSharper disable once PossibleNullReferenceException
                    await Task.Delay(250, token).ConfigureAwait(false);
                    if (token.IsCancellationRequested) return false;
                    serviceController.Refresh();
                }
            }
            return true;
        }
开发者ID:webappsuk,项目名称:CoreLibraries,代码行数:23,代码来源:Controller.cs

示例2: RunAsync

        public static async Task RunAsync(
            [NotNull] string description,
            [CanBeNull] NamedPipeServerInfo service = null,
            CancellationToken token = default(CancellationToken))
        {
            if (description == null) throw new ArgumentNullException("description");

            if (!ConsoleHelper.IsConsole)
                return;

            try
            {
                Log.SetTrace(validLevels: LoggingLevels.None);
                Log.SetConsole(Log.ShortFormat);

                Console.Title = description;
                NamedPipeClient client = null;
                while (client == null)
                {
                    token.ThrowIfCancellationRequested();
                    while (service == null ||
                           !service.IsValid)
                    {
                        Console.Clear();
                        NamedPipeServerInfo[] services = null;
                        await Log.Flush(token).ConfigureAwait(false);

                        // ReSharper disable once AssignNullToNotNullAttribute
                        ConsoleTextWriter.Default.WriteLine(ClientResources.ConsoleClient_RunAsync_ScanningForService);
                        while (services == null ||
                               services.Length < 1)
                        {
                            services = NamedPipeClient.GetServices().ToArray();
                            if (Console.KeyAvailable)
                                return;
                            // ReSharper disable once PossibleNullReferenceException
                            await Task.Delay(500, token).ConfigureAwait(false);
                            token.ThrowIfCancellationRequested();
                        }

                        if (services.Length > 0)
                            WriteServerList(services);

                        // ReSharper disable once AssignNullToNotNullAttribute
                        ConsoleTextWriter.Default.WriteLine(ClientResources.ConsoleClient_RunAsync_EnterServiceName);
                        string serviceName = Console.ReadLine();
                        service = !string.IsNullOrWhiteSpace(serviceName)
                            ? NamedPipeClient.FindService(serviceName)
                            : NamedPipeClient.GetServices().FirstOrDefault();
                    }

                    Console.Clear();
                    // ReSharper disable once AssignNullToNotNullAttribute
                    ConsoleTextWriter.Default.WriteLine(
                        ClientResources.ConsoleClient_RunAsync_ConnectingToService,
                        service.Name);

                    try
                    {
                        // TODO Remove constant timeout
                        using (ITokenSource tokenSource = token.WithTimeout(10000))
                            client =
                                await
                                    NamedPipeClient.Connect(
                                        description,
                                        service,
                                        OnReceive,
                                        tokenSource.Token)
                                        .ConfigureAwait(false);
                    }
                    catch (TaskCanceledException)
                    {
                        // ReSharper disable once AssignNullToNotNullAttribute
                        ConsoleTextWriter.Default.WriteLine(
                            ClientResources.ConsoleClient_RunAsync_TimedOut,
                            service.Name);
                        // ReSharper disable once AssignNullToNotNullAttribute
                        ConsoleTextWriter.Default.WriteLine(ClientResources.ConsoleClient_RunAsync_PressAnyKeyContinue);
                        client = null;
                        service = null;
                        Console.ReadKey(true);
                    }
                }

                // ReSharper disable once AssignNullToNotNullAttribute
                Console.Title = string.Format(
                    ClientResources.ConsoleClient_RunAsync_ConnectedTitle,
                    description,
                    service.Name);
                _connected.WriteToConsole(
                    null,
                    new Dictionary<string, object>
                    {
                        { "ServiceName", client.ServiceName }
                    });

                // ReSharper disable once PossibleNullReferenceException
                await Task.Delay(1100, token).ConfigureAwait(false);
                await Log.Flush(token).ConfigureAwait(false);

//.........这里部分代码省略.........
开发者ID:webappsuk,项目名称:CoreLibraries,代码行数:101,代码来源:ConsoleClient.cs


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