From 93b7aec9295f4512cffa409464be313a4f1f25c3 Mon Sep 17 00:00:00 2001 From: Jerker Dahlblom Date: Wed, 6 Mar 2024 15:33:04 +0200 Subject: [PATCH] Improve dispose of DCS-BIOS --- src/DCS-BIOS/DCSBIOS.cs | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/DCS-BIOS/DCSBIOS.cs b/src/DCS-BIOS/DCSBIOS.cs index 0f7137e..67d814e 100644 --- a/src/DCS-BIOS/DCSBIOS.cs +++ b/src/DCS-BIOS/DCSBIOS.cs @@ -108,13 +108,10 @@ private void Dispose(bool disposing) { if (disposing) { - _udpReceiveClient?.Dispose(); - _udpSendClient?.Dispose(); - _dcsProtocolParser?.Dispose(); Shutdown(); } } - + public void Startup() { try @@ -124,6 +121,8 @@ public void Startup() return; } + _udpReceiveThrottleAutoResetEvent = new(false); + _dcsProtocolParser = DCSBIOSProtocolParser.GetParser(); _ipEndPointReceiverUdp = new IPEndPoint(IPAddress.Any, ReceivePortUdp); @@ -169,14 +168,25 @@ public void Shutdown() try { _isRunning = false; - _udpReceiveThrottleAutoResetEvent.Set(); - _udpReceiveClient?.Close(); - _dcsProtocolParser?.Shutdown(); + _udpReceiveThrottleTimer.Stop(); + + _udpReceiveThrottleAutoResetEvent?.Set(); + _udpReceiveThrottleAutoResetEvent?.Close(); + _udpReceiveThrottleAutoResetEvent?.Dispose(); + _udpReceiveThrottleAutoResetEvent = null; + + _udpReceiveClient?.Close(); + _udpReceiveClient?.Dispose(); _udpReceiveClient = null; - _udpReceiveClient = null; + + _udpSendClient?.Close(); + _udpSendClient?.Dispose(); + _udpSendClient = null; + + _dcsProtocolParser?.Shutdown(); + _dcsProtocolParser?.Dispose(); _dcsProtocolParser = null; - _udpReceiveThrottleTimer.Stop(); } catch (Exception ex) { @@ -187,7 +197,7 @@ public void Shutdown() private void UdpReceiveThrottleTimer_Elapsed(object sender, ElapsedEventArgs e) { - _udpReceiveThrottleAutoResetEvent.Set(); + _udpReceiveThrottleAutoResetEvent?.Set(); } public void ReceiveDataUdp()