@@ -271,47 +271,39 @@ public async Task SendCommandRequest(object request)
271271 await SendCommandRequestPackage ( CommandContext . GenerateNewId ( ) , typeName , requestContent ) . ConfigureAwait ( false ) ;
272272 }
273273
274- protected void BeginHeartBeat ( CancellationToken cancellationToken )
274+ protected async Task BeginHeartBeat ( CancellationToken cancellationToken )
275275 {
276- if ( options . HeartBeatInterval > 0 )
277- _ = Task . Delay ( options . HeartBeatInterval , cancellationToken ) . ContinueWith ( t =>
276+ if ( options . HeartBeatInterval < 0 )
277+ return ;
278+ while ( ! cancellationToken . IsCancellationRequested )
279+ {
280+ await Task . Delay ( options . HeartBeatInterval , cancellationToken ) ;
281+ if ( QpPackageHandler_Stream == null )
282+ return ;
283+ var lastSendPackageToNowSeconds = ( DateTime . Now - lastSendPackageTime ) . TotalMilliseconds ;
284+ //如果离最后一次发送数据包的时间大于心跳间隔,则发送心跳包
285+ if ( lastSendPackageToNowSeconds > options . HeartBeatInterval )
278286 {
279- if ( t . IsCanceled )
280- return ;
281- if ( QpPackageHandler_Stream == null )
282- return ;
283-
284- var lastSendPackageToNowSeconds = ( DateTime . Now - lastSendPackageTime ) . TotalMilliseconds ;
285-
286- //如果离最后一次发送数据包的时间大于心跳间隔,则发送心跳包
287- if ( lastSendPackageToNowSeconds > options . HeartBeatInterval )
288- {
289- _ = SendHeartbeatPackage ( ) ;
290- }
291- BeginHeartBeat ( cancellationToken ) ;
292- } ) ;
287+ await SendHeartbeatPackage ( ) ;
288+ }
289+ }
293290 }
294291
295- protected void BeginNetstat ( CancellationToken cancellationToken )
292+ protected async Task BeginNetstat ( CancellationToken cancellationToken )
296293 {
297294 if ( ! options . EnableNetstat )
298295 return ;
299- if ( cancellationToken . IsCancellationRequested )
300- return ;
301296
302- long preBytesReceived = BytesReceived ;
303- long preBytesSent = BytesSent ;
304-
305- _ = Task . Delay ( TimeSpan . FromSeconds ( 1 ) , cancellationToken ) . ContinueWith ( t =>
297+ while ( ! cancellationToken . IsCancellationRequested )
306298 {
307- if ( t . IsCanceled )
308- return ;
299+ long preBytesReceived = BytesReceived ;
300+ long preBytesSent = BytesSent ;
301+ await Task . Delay ( TimeSpan . FromSeconds ( 1 ) , cancellationToken ) ;
309302 if ( QpPackageHandler_Stream == null )
310303 return ;
311304 BytesReceivedPerSec = BytesReceived - preBytesReceived ;
312305 BytesSentPerSec = BytesSent - preBytesSent ;
313- BeginNetstat ( cancellationToken ) ;
314- } ) ;
306+ }
315307 }
316308
317309
0 commit comments