14
14
*/
15
15
16
16
using System ;
17
- using System . Collections . Concurrent ;
18
17
using System . Diagnostics ;
19
18
using System . Net ;
20
19
using System . Threading ;
@@ -36,6 +35,7 @@ internal sealed class ServerMonitor : IServerMonitor
36
35
private readonly EndPoint _endPoint ;
37
36
private HeartbeatDelay _heartbeatDelay ;
38
37
private readonly object _lock = new object ( ) ;
38
+ private readonly CancellationToken _monitorCancellationToken ; // used to cancel the entire monitor
39
39
private readonly CancellationTokenSource _monitorCancellationTokenSource ; // used to cancel the entire monitor
40
40
private readonly IRoundTripTimeMonitor _roundTripTimeMonitor ;
41
41
private readonly ServerApi _serverApi ;
@@ -101,7 +101,8 @@ public ServerMonitor(
101
101
eventSubscriber . TryGetEventHandler ( out _sdamInformationEventHandler ) ;
102
102
_serverApi = serverApi ;
103
103
104
- _heartbeatCancellationTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( _monitorCancellationTokenSource . Token ) ;
104
+ _monitorCancellationToken = _monitorCancellationTokenSource . Token ;
105
+ _heartbeatCancellationTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( _monitorCancellationToken ) ;
105
106
}
106
107
107
108
public ServerDescription Description => Interlocked . CompareExchange ( ref _currentDescription , null , null ) ;
@@ -118,7 +119,7 @@ public void CancelCurrentCheck()
118
119
{
119
120
_heartbeatCancellationTokenSource . Cancel ( ) ;
120
121
_heartbeatCancellationTokenSource . Dispose ( ) ;
121
- _heartbeatCancellationTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( _monitorCancellationTokenSource . Token ) ;
122
+ _heartbeatCancellationTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( _monitorCancellationToken ) ;
122
123
// the previous hello or legacy hello cancellation token is still cancelled
123
124
124
125
toDispose = _connection ;
@@ -147,20 +148,13 @@ public void Initialize()
147
148
if ( _state . TryChange ( State . Initial , State . Open ) )
148
149
{
149
150
_roundTripTimeMonitor . Start ( ) ;
150
- _serverMonitorThread = new Thread ( ThreadStart ) { IsBackground = true } ;
151
- _serverMonitorThread . Start ( ) ;
151
+ _serverMonitorThread = new Thread ( new ParameterizedThreadStart ( ThreadStart ) ) { IsBackground = true } ;
152
+ _serverMonitorThread . Start ( _monitorCancellationToken ) ;
152
153
}
153
154
154
- void ThreadStart ( )
155
+ void ThreadStart ( object monitorCancellationToken )
155
156
{
156
- try
157
- {
158
- MonitorServer ( ) ;
159
- }
160
- catch ( OperationCanceledException )
161
- {
162
- // ignore OperationCanceledException
163
- }
157
+ MonitorServer ( ( CancellationToken ) monitorCancellationToken ) ;
164
158
}
165
159
}
166
160
@@ -218,10 +212,9 @@ private IConnection InitializeConnection(CancellationToken cancellationToken) //
218
212
return connection ;
219
213
}
220
214
221
- private void MonitorServer ( )
215
+ private void MonitorServer ( CancellationToken monitorCancellationToken )
222
216
{
223
217
var metronome = new Metronome ( _serverMonitorSettings . HeartbeatInterval ) ;
224
- var monitorCancellationToken = _monitorCancellationTokenSource . Token ;
225
218
226
219
while ( ! monitorCancellationToken . IsCancellationRequested )
227
220
{
0 commit comments