Skip to content

Commit

Permalink
Fix caching issues.
Browse files Browse the repository at this point in the history
Fixes #323
  • Loading branch information
Antoine Aflalo committed Feb 26, 2019
1 parent 2ad7511 commit 2c7576a
Showing 1 changed file with 15 additions and 28 deletions.
43 changes: 15 additions & 28 deletions SoundSwitch/Model/CachedAudioDeviceLister.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ namespace SoundSwitch.Model
{
public class CachedAudioDeviceLister : IAudioDeviceLister
{

/// <inheritdoc />
public ICollection<DeviceFullInfo> PlaybackDevices { get; private set; }

/// <inheritdoc />
public ICollection<DeviceFullInfo> RecordingDevices { get; private set; }

Expand All @@ -47,35 +47,22 @@ private void DeviceChanged(object sender, DeviceChangedEventBase e)

private void Refresh()
{
if (!Monitor.TryEnter(this, 500))
var playbackTask = Task<ICollection<DeviceFullInfo>>.Factory.StartNew((() =>
{
return;
}
try
{

var playbackTask = Task<ICollection<DeviceFullInfo>>.Factory.StartNew((() =>
using (var enumerator = new MMDeviceEnumerator())
{
using (var enumerator = new MMDeviceEnumerator())
{
return CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Render, _state));
}
}));
var recordingTask = Task<ICollection<DeviceFullInfo>>.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<ICollection<DeviceFullInfo>>.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<DeviceFullInfo> CreateDeviceList(MMDeviceCollection collection)
Expand All @@ -90,13 +77,13 @@ private static ICollection<DeviceFullInfo> 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;
Expand Down

0 comments on commit 2c7576a

Please sign in to comment.