本文整理汇总了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(
//.........这里部分代码省略.........