Skip to content

Commit 18aa0b0

Browse files
committed
fix cancellation exception
1 parent 39a8228 commit 18aa0b0

File tree

1 file changed

+27
-23
lines changed

1 file changed

+27
-23
lines changed

Project-Aurora/AuroraCommon/Utils/MemorySharedEventThread.cs

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ private Thread CreateThread()
7373
_semaphore.Wait(CancelToken.Token);
7474
ThreadCallback();
7575
}
76+
catch (OperationCanceledException)
77+
{
78+
// end the thread
79+
}
7680
finally
7781
{
7882
_semaphore.Release();
@@ -86,35 +90,35 @@ private Thread CreateThread()
8690
thread.Start();
8791
Thread.Yield();
8892
return thread;
89-
90-
void ThreadCallback(){
91-
if (_handles.Length <= 1)
93+
}
94+
95+
private void ThreadCallback(){
96+
if (_handles.Length <= 1)
97+
{
98+
// stop thread if only handle is cancel token
99+
return;
100+
}
101+
while (true)
102+
{
103+
if (CancelToken.IsCancellationRequested)
92104
{
93-
// stop thread if only handle is cancel token
94105
return;
95106
}
96-
while (true)
107+
var i = WaitHandle.WaitAny(_handles);
108+
switch (i)
97109
{
98-
if (CancelToken.IsCancellationRequested)
99-
{
110+
case 0:
100111
return;
101-
}
102-
var i = WaitHandle.WaitAny(_handles);
103-
switch (i)
104-
{
105-
case 0:
106-
return;
107-
default:
108-
if (i >= _handles.Length)
109-
{
110-
break;
111-
}
112-
Task.Run(() =>
113-
{
114-
_actions[i].Invoke();
115-
});
112+
default:
113+
if (i >= _handles.Length)
114+
{
116115
break;
117-
}
116+
}
117+
Task.Run(() =>
118+
{
119+
_actions[i].Invoke();
120+
});
121+
break;
118122
}
119123
}
120124
}

0 commit comments

Comments
 (0)