Skip to content

Commit 312200a

Browse files
committed
Improved SemaphoreSlim handling
1 parent 44e7a6b commit 312200a

File tree

3 files changed

+20
-20
lines changed

3 files changed

+20
-20
lines changed

RICADO.Omron/Channels/EthernetChannel.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ internal async Task<ProcessRequestResult> ProcessRequestAsync(FINSRequest reques
8787

8888
while (attempts <= retries)
8989
{
90+
if (!_semaphore.Wait(0))
91+
{
92+
await _semaphore.WaitAsync(cancellationToken);
93+
}
94+
9095
try
9196
{
92-
if (!_semaphore.Wait(0))
93-
{
94-
await _semaphore.WaitAsync(cancellationToken);
95-
}
96-
9797
if (attempts > 0)
9898
{
9999
await DestroyAndInitializeClient(timeout, cancellationToken);
@@ -149,13 +149,13 @@ internal async Task<ProcessRequestResult> ProcessRequestAsync(FINSRequest reques
149149
{
150150
if(e.Message.Contains("Service ID") && responseMessage.Length >= 9 && responseMessage.Span[9] != request.ServiceID)
151151
{
152-
try
152+
if (!_semaphore.Wait(0))
153153
{
154-
if (!_semaphore.Wait(0))
155-
{
156-
await _semaphore.WaitAsync(cancellationToken);
157-
}
154+
await _semaphore.WaitAsync(cancellationToken);
155+
}
158156

157+
try
158+
{
159159
await PurgeReceiveBuffer(timeout, cancellationToken);
160160
}
161161
catch

RICADO.Omron/Channels/EthernetTCPChannel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,13 @@ public override void Dispose()
8181

8282
internal override async Task InitializeAsync(int timeout, CancellationToken cancellationToken)
8383
{
84-
try
84+
if (!Semaphore.Wait(0))
8585
{
86-
if(!Semaphore.Wait(0))
87-
{
88-
await Semaphore.WaitAsync(cancellationToken);
89-
}
86+
await Semaphore.WaitAsync(cancellationToken);
87+
}
9088

89+
try
90+
{
9191
destroyClient();
9292

9393
await initializeClient(timeout, cancellationToken);

RICADO.Omron/Channels/EthernetUDPChannel.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,13 @@ public override void Dispose()
5151

5252
internal override async Task InitializeAsync(int timeout, CancellationToken cancellationToken)
5353
{
54-
try
54+
if (!Semaphore.Wait(0))
5555
{
56-
if (!Semaphore.Wait(0))
57-
{
58-
await Semaphore.WaitAsync(cancellationToken);
59-
}
56+
await Semaphore.WaitAsync(cancellationToken);
57+
}
6058

59+
try
60+
{
6161
destroyClient();
6262

6363
await initializeClient(timeout, cancellationToken);

0 commit comments

Comments
 (0)