1414*/
1515
1616using System ;
17- using System . Collections . Concurrent ;
1817using System . Diagnostics ;
1918using System . Net ;
2019using System . Threading ;
@@ -36,6 +35,7 @@ internal sealed class ServerMonitor : IServerMonitor
3635 private readonly EndPoint _endPoint ;
3736 private HeartbeatDelay _heartbeatDelay ;
3837 private readonly object _lock = new object ( ) ;
38+ private readonly CancellationToken _monitorCancellationToken ; // used to cancel the entire monitor
3939 private readonly CancellationTokenSource _monitorCancellationTokenSource ; // used to cancel the entire monitor
4040 private readonly IRoundTripTimeMonitor _roundTripTimeMonitor ;
4141 private readonly ServerApi _serverApi ;
@@ -101,7 +101,8 @@ public ServerMonitor(
101101 eventSubscriber . TryGetEventHandler ( out _sdamInformationEventHandler ) ;
102102 _serverApi = serverApi ;
103103
104- _heartbeatCancellationTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( _monitorCancellationTokenSource . Token ) ;
104+ _monitorCancellationToken = _monitorCancellationTokenSource . Token ;
105+ _heartbeatCancellationTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( _monitorCancellationToken ) ;
105106 }
106107
107108 public ServerDescription Description => Interlocked . CompareExchange ( ref _currentDescription , null , null ) ;
@@ -118,7 +119,7 @@ public void CancelCurrentCheck()
118119 {
119120 _heartbeatCancellationTokenSource . Cancel ( ) ;
120121 _heartbeatCancellationTokenSource . Dispose ( ) ;
121- _heartbeatCancellationTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( _monitorCancellationTokenSource . Token ) ;
122+ _heartbeatCancellationTokenSource = CancellationTokenSource . CreateLinkedTokenSource ( _monitorCancellationToken ) ;
122123 // the previous hello or legacy hello cancellation token is still cancelled
123124
124125 toDispose = _connection ;
@@ -147,20 +148,13 @@ public void Initialize()
147148 if ( _state . TryChange ( State . Initial , State . Open ) )
148149 {
149150 _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 ) ;
152153 }
153154
154- void ThreadStart ( )
155+ void ThreadStart ( object monitorCancellationToken )
155156 {
156- try
157- {
158- MonitorServer ( ) ;
159- }
160- catch ( OperationCanceledException )
161- {
162- // ignore OperationCanceledException
163- }
157+ MonitorServer ( ( CancellationToken ) monitorCancellationToken ) ;
164158 }
165159 }
166160
@@ -218,10 +212,9 @@ private IConnection InitializeConnection(CancellationToken cancellationToken) //
218212 return connection ;
219213 }
220214
221- private void MonitorServer ( )
215+ private void MonitorServer ( CancellationToken monitorCancellationToken )
222216 {
223217 var metronome = new Metronome ( _serverMonitorSettings . HeartbeatInterval ) ;
224- var monitorCancellationToken = _monitorCancellationTokenSource . Token ;
225218
226219 while ( ! monitorCancellationToken . IsCancellationRequested )
227220 {
0 commit comments