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


C# CancellationToken.ToCancelable方法代码示例

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


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

示例1: ConnectedCommand

            /// <summary>
            /// Initializes a new instance of the <see cref="ConnectedCommand" /> class.
            /// </summary>
            /// <param name="connectionGuid">The connection unique identifier.</param>
            /// <param name="service">The service.</param>
            /// <param name="connection">The connection.</param>
            /// <param name="request">The request.</param>
            /// <param name="token">The cancellation token.</param>
            public ConnectedCommand(
                Guid connectionGuid,
                [NotNull] BaseService service,
                [NotNull] NamedPipeConnection connection,
                [NotNull] CommandRequest request,
                CancellationToken token = default(CancellationToken))
            {
                ConnectionGuid = connectionGuid;
                _connection = connection;
                _request = request;
                _cancellationTokenSource = token.ToCancelable();
                ID = _request.ID;

                token = _cancellationTokenSource.Token;

                Task.Run(
                    async () =>
                    {
                        try
                        {
                            do
                            {
                                // ReSharper disable once PossibleNullReferenceException
                                await Task.Delay(250, token).ConfigureAwait(false);
                                if (token.IsCancellationRequested) return;
                                await Flush(0, token).ConfigureAwait(false);
                            } while (true);
                        }
                        catch (TaskCanceledException)
                        {
                        }
                    },
                    token);
                // Kick of task to run command.
                Task.Run(
                    async () =>
                    {
                        Exception exception = null;
                        bool cancelled = false;
                        try
                        {
                            await
                                service.ExecuteAsync(ConnectionGuid, _request.CommandLine, this, token)
                                    .ConfigureAwait(false);
                            if (!token.IsCancellationRequested)
                                await Flush(-1, token).ConfigureAwait(false);
                            else
                                cancelled = true;
                        }
                        catch (OperationCanceledException)
                        {
                            cancelled = true;
                        }
                        catch (Exception e)
                        {
                            exception = e;
                        }

                        if (cancelled)
                            using (await _flushLock.LockAsync(token).ConfigureAwait(false))
                            {
                                try
                                {
                                    using (CancellationTokenSource cts = Constants.FireAndForgetTokenSource)
                                        await connection.Send(
                                            new CommandCancelResponse(
                                                _cancelRequest != null ? _cancelRequest.ID : Guid.Empty,
                                                ID),
                                            cts.Token)
                                            .ConfigureAwait(false);
                                }
                                catch (OperationCanceledException)
                                {
                                }
                                return;
                            }

                        if (exception != null)
                            try
                            {
                                await Flush(0, token).ConfigureAwait(false);
                                _builder.Append(exception.Message);
                                await Flush(-2, token).ConfigureAwait(false);
                            }
                                // ReSharper disable once EmptyGeneralCatchClause
                            catch
                            {
                            }

                        Dispose(true);
                    },
                    token);
//.........这里部分代码省略.........
开发者ID:webappsuk,项目名称:CoreLibraries,代码行数:101,代码来源:ConnectedCommand.cs


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