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


C# CancellationToken.AddTimeout方法代码示例

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


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

示例1: WriteMessageAsync

        /// <summary>
        /// Write a message to the SSL stream
        /// </summary>
        /// <param name="type">Type of message</param>
        /// <param name="messageBytes">Byte array of serialized message</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests</param>
        /// <returns>Empty task</returns>
        private async Task WriteMessageAsync(MessageType type, byte[] messageBytes, CancellationToken cancellationToken)
        {
            var typeBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder((short)type));
            var sizeBytes = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(messageBytes.Length));

            await this.writeSemaphore.WaitAsync(cancellationToken).ConfigureAwait(false);
            using (var cts = cancellationToken.AddTimeout(WriteTimeout))
            {
                await this.sslStream.WriteAsync(typeBytes, 0, typeBytes.Length, cts.Token)
                    .HandleTimeout(cts.Token).ConfigureAwait(false);
                await this.sslStream.WriteAsync(sizeBytes, 0, sizeBytes.Length, cts.Token)
                    .HandleTimeout(cts.Token).ConfigureAwait(false);
                await this.sslStream.WriteAsync(messageBytes, 0, messageBytes.Length, cts.Token)
                    .HandleTimeout(cts.Token).ConfigureAwait(false);
            }

            this.writeSemaphore.Release();
        }
开发者ID:sshipsey,项目名称:Mumble.NET,代码行数:25,代码来源:Connection.cs

示例2: ReadMessageAsync

        /// <inheritdoc />
        public async Task<IMessage> ReadMessageAsync(CancellationToken cancellationToken, int timeout = Timeout.Infinite)
        {
            var headerBytes = new byte[6];
            using (var cts = cancellationToken.AddTimeout(timeout))
            {
                await this.sslStream.ReadAsync(headerBytes, 0, headerBytes.Length, cts.Token)
                    .HandleTimeout(cts.Token).ConfigureAwait(false);
            }

            var type = IPAddress.NetworkToHostOrder(BitConverter.ToInt16(headerBytes, 0));
            var size = IPAddress.NetworkToHostOrder(BitConverter.ToInt32(headerBytes, 2));

            var messageBytes = new byte[size];
            using (var cts = cancellationToken.AddTimeout(timeout))
            {
                await this.sslStream.ReadAsync(messageBytes, 0, size, cts.Token)
                    .HandleTimeout(cts.Token).ConfigureAwait(false);
            }

            if (type == (int)MessageType.UDPTunnel)
            {
                return new UDPTunnel.Builder
                {
                    Packet = ByteString.CopyFrom(messageBytes)
                }.Build();
            }
            else
            {
                return this.messageFactory.Deserialize((MessageType)type, ByteString.CopyFrom(messageBytes));
            }
        }
开发者ID:sshipsey,项目名称:Mumble.NET,代码行数:32,代码来源:Connection.cs


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