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


C# SemaphoreSlim.GetDisposable方法代码示例

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


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

示例1: SendReliableMessage

 public async Task<uint> SendReliableMessage(
     string destination,
     byte[] data,
     IProgress<OutMessageProgressInfo> progress = null,
     uint messageId = 0,
     uint replyToMessageId = 0,
     byte messageKind = 0,
     CancellationToken token = default(CancellationToken)
 )
 {
     using (_stateHelper.GetFuncWrapper())
     {
         try
         {
             Assert.True(SamHelper.IsDestinationStringValid(destination));
             Assert.NotNull(data);
             Assert.InRange(data.Length, 1, _settings.MaxMesageLength);
             if (messageId == 0)
                 messageId = await GetNextOutMessageId().ConfigureAwait(false);
             DestinationInfo destinationInfo;
             using (await _destinationDbLockSem.GetDisposable(token: token).ConfigureAwait(false))
             {
                 if (!_destinationDb.ContainsKey(destination))
                     _destinationDb.Add(destination, new DestinationInfo());
                 destinationInfo = _destinationDb[destination];
             }
             OutMessageInfo outMessageInfo;
             using (await destinationInfo.OutMessagesDbLockSem.GetDisposable(token: token).ConfigureAwait(false))
             {
                 if (
                     destinationInfo.OutMessagesDb.ContainsKey(messageId)
                 )
                     throw new EnumException<SendReliableMessageExcs>(
                         SendReliableMessageExcs.ExistMessageId
                     );
                 outMessageInfo = new OutMessageInfo(
                     data,
                     replyToMessageId,
                     _settings.MaxBlockSize,
                     progress
                 );
                 destinationInfo.OutMessagesDb.Add(messageId, outMessageInfo);
             }
             try
             {
                 uint blocksCount = outMessageInfo.BlockCount;
                 /*
                  * Handshake
                  */
                 using (
                     var compositeTokenSource = CancellationTokenSource.CreateLinkedTokenSource(
                         _cts.Token,
                         token
                     )
                 )
                 {
                     try
                     {
                         var messageStatusTask =
                             MessageStatusReceived
                                 .Where(
                                     x =>
                                         x.Destination == destination
                                         && x.MessageId == messageId
                                 )
                                 .FirstAsync()
                                 .ToTask(compositeTokenSource.Token);
                         byte[] messageHash = GetMessageHash(
                             messageId,
                             replyToMessageId,
                             messageKind,
                             data
                         );
                         for (
                             outMessageInfo.HandshakeAttemptNo = 0;
                             outMessageInfo.HandshakeAttemptNo
                                 <= _settings.HandshakeAttemptCount;
                             outMessageInfo.HandshakeAttemptNo++
                         )
                         {
                             if (
                                 outMessageInfo.HandshakeAttemptNo
                                 == _settings.HandshakeAttemptCount
                             )
                                 throw new EnumException<SendReliableMessageExcs>(
                                     SendReliableMessageExcs.HandshakeTimeout
                                 );
                             await SendHandshakeStart(
                                 destination,
                                 messageId,
                                 replyToMessageId,
                                 (uint)data.Length,
                                 outMessageInfo.BlockSize,
                                 messageHash,
                                 outMessageInfo.Data[0],
                                 messageKind
                             ).ConfigureAwait(false);
                             var waitTasks = await Task.WhenAny(
                                 messageStatusTask,
                                 Task.Delay(
//.........这里部分代码省略.........
开发者ID:z0rg1nc,项目名称:SamHelperLib,代码行数:101,代码来源:ReliableSamHelper_Out.cs


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