From 42f87510f78c6ef399d4e6e89e62c46c745e39c6 Mon Sep 17 00:00:00 2001 From: Dexter M Haslem Date: Sat, 4 Jun 2022 00:36:28 -0600 Subject: [PATCH] fix potential freezes and crashes in instant connect/disconnect scenarios couple things discovered from network issues causing instant connect/disconnect: - Join() could block forever in some cases, so try to slam shut first and not block the wpf dispatcher - captureThread could be nulled out while dispose was executing on other thread --- NAudio/Wave/WaveInputs/WasapiCapture.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/NAudio/Wave/WaveInputs/WasapiCapture.cs b/NAudio/Wave/WaveInputs/WasapiCapture.cs index 0f332bae7..dc4e54eb7 100644 --- a/NAudio/Wave/WaveInputs/WasapiCapture.cs +++ b/NAudio/Wave/WaveInputs/WasapiCapture.cs @@ -250,7 +250,8 @@ private void CaptureThread(AudioClient client) client.Stop(); // don't dispose - the AudioClient only gets disposed when WasapiCapture is disposed } - captureThread = null; + // dont nuke our captureThread reference here, dispose also checks it + // captureThread = null; captureState = CaptureState.Stopped; RaiseRecordingStopped(exception); } @@ -356,7 +357,11 @@ public void Dispose() StopRecording(); if (captureThread != null) { - captureThread.Join(); + if (captureThread.IsAlive) + { + captureThread.Abort(); + captureThread.Join(); + } captureThread = null; } if (audioClient != null)