From 2c7576a1a3e6fa5901d4ec20ff120673a562be97 Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Tue, 26 Feb 2019 14:30:00 -0500 Subject: [PATCH] Fix caching issues. Fixes #323 --- SoundSwitch/Model/CachedAudioDeviceLister.cs | 43 +++++++------------- 1 file changed, 15 insertions(+), 28 deletions(-) diff --git a/SoundSwitch/Model/CachedAudioDeviceLister.cs b/SoundSwitch/Model/CachedAudioDeviceLister.cs index 7e502a43b1..41adc0e879 100644 --- a/SoundSwitch/Model/CachedAudioDeviceLister.cs +++ b/SoundSwitch/Model/CachedAudioDeviceLister.cs @@ -25,9 +25,9 @@ namespace SoundSwitch.Model { public class CachedAudioDeviceLister : IAudioDeviceLister { - /// public ICollection PlaybackDevices { get; private set; } + /// public ICollection RecordingDevices { get; private set; } @@ -47,35 +47,22 @@ private void DeviceChanged(object sender, DeviceChangedEventBase e) private void Refresh() { - if (!Monitor.TryEnter(this, 500)) + var playbackTask = Task>.Factory.StartNew((() => { - return; - } - try - { - - var playbackTask = Task>.Factory.StartNew((() => + using (var enumerator = new MMDeviceEnumerator()) { - using (var enumerator = new MMDeviceEnumerator()) - { - return CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Render, _state)); - } - })); - var recordingTask = Task>.Factory.StartNew((() => - { - using (var enumerator = new MMDeviceEnumerator()) - { - return CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Capture, _state)); - } - })); - PlaybackDevices = playbackTask.Result; - RecordingDevices = recordingTask.Result; - } - finally + return CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Render, _state)); + } + })); + var recordingTask = Task>.Factory.StartNew((() => { - Monitor.Exit(this); - } - + using (var enumerator = new MMDeviceEnumerator()) + { + return CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Capture, _state)); + } + })); + PlaybackDevices = playbackTask.Result; + RecordingDevices = recordingTask.Result; } private static ICollection CreateDeviceList(MMDeviceCollection collection) @@ -90,13 +77,13 @@ private static ICollection CreateDeviceList(MMDeviceCollection c { continue; } + sortedDevices.Add(device.FriendlyName, deviceInfo); } catch (Exception) { Log.Warning("Can't get name of device {device}", device.ID); } - } return sortedDevices.Values;