Skip to content

Commit 77abc9b

Browse files
committed
feat: improve CancellationToken usage
1 parent 393e4bc commit 77abc9b

File tree

3 files changed

+14
-6
lines changed

3 files changed

+14
-6
lines changed

src/ByteSync.Client/Repositories/CloudSessionConnectionRepository.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ public void SetCreateSessionError(CreateSessionError createSessionError)
127127

128128
public void SetJoinSessionError(JoinSessionError joinSessionError)
129129
{
130+
CancellationTokenSource.Cancel();
131+
130132
_joinSessionError.OnNext(joinSessionError);
131133
}
132134

src/ByteSync.Client/Services/Sessions/Connecting/Joining/CloudSessionPasswordExchangeKeyGivenService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ public async Task Process(GiveCloudSessionPasswordExchangeKeyParameters request)
8080
else
8181
{
8282
await _cloudSessionConnectionRepository.WaitOrThrowAsync(request.SessionId,
83-
data => data.WaitForJoinSessionEvent, data => data.WaitTimeSpan, "Join session failed");
83+
data => data.WaitForJoinSessionEvent, data => data.WaitTimeSpan, "Join session failed",
84+
_cloudSessionConnectionRepository.CancellationToken);
8485
}
8586
}
8687
else

src/ByteSync.Common/Controls/BaseRepository.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ public bool Wait(string dataId, Func<T, EventWaitHandle> func, Func<T, TimeSpan?
186186
CheckAndRun(dataId, myFunc);
187187

188188
timeout ??= new TimeSpan(-1);
189-
List<WaitHandle> waitHandles = new List<WaitHandle> {waitHandle!};
189+
List<WaitHandle> waitHandles = [waitHandle!];
190+
190191
if (endEvent != null)
191192
{
192193
waitHandles.Add(endEvent);
@@ -202,16 +203,20 @@ public bool Wait(string dataId, Func<T, EventWaitHandle> func, Func<T, TimeSpan?
202203
{
203204
return true;
204205
}
205-
if (index == 1)
206+
if (waitHandles[index] == endEvent)
206207
{
207208
Log.Error("Wait: Process has ended");
208209
return false;
209210
}
210-
else
211+
if (waitHandles[index] == cancellationToken.WaitHandle)
211212
{
212-
Log.Error("Wait: timeout has occured");
213-
return false;
213+
if (cancellationToken.IsCancellationRequested)
214+
{
215+
return true;
216+
}
214217
}
218+
Log.Error("Wait: timeout has occured");
219+
return false;
215220
}
216221

217222
private void CheckAndRun(string? dataId, Action<T> func)

0 commit comments

Comments
 (0)