From 65892c48315adb4a01ce9dbed06c8432c55882db Mon Sep 17 00:00:00 2001 From: Antoine Aflalo Date: Tue, 12 Nov 2019 19:32:11 -0500 Subject: [PATCH] Change device lister logic to use async await See #357 #358 #359 #360 #361 --- .../.idea.SoundSwitch/.idea/contentModel.xml | 2 +- .../SoundSwitch.Audio.Manager.csproj | 6 ++-- .../Audio/Lister/CachedAudioDeviceLister.cs | 25 ++++++-------- SoundSwitch/Model/IAudioDeviceLister.cs | 3 ++ SoundSwitch/Program.cs | 22 ++++++------ SoundSwitch/SoundSwitch.csproj | 34 +++++++++---------- SoundSwitch/UI/Forms/Settings.cs | 20 ++++++----- SoundSwitch/Util/TrayIcon.cs | 9 +++-- 8 files changed, 66 insertions(+), 55 deletions(-) diff --git a/.idea/.idea.SoundSwitch/.idea/contentModel.xml b/.idea/.idea.SoundSwitch/.idea/contentModel.xml index d3371bee92..0f93014500 100644 --- a/.idea/.idea.SoundSwitch/.idea/contentModel.xml +++ b/.idea/.idea.SoundSwitch/.idea/contentModel.xml @@ -8,7 +8,6 @@ - @@ -220,6 +219,7 @@ + diff --git a/SoundSwitch.Audio.Manager/SoundSwitch.Audio.Manager.csproj b/SoundSwitch.Audio.Manager/SoundSwitch.Audio.Manager.csproj index 4dd918e5ea..92b538d6c7 100644 --- a/SoundSwitch.Audio.Manager/SoundSwitch.Audio.Manager.csproj +++ b/SoundSwitch.Audio.Manager/SoundSwitch.Audio.Manager.csproj @@ -30,11 +30,13 @@ 4 - - ..\packages\NAudio.1.8.5\lib\net35\NAudio.dll + + ..\packages\NAudio.1.9.0\lib\net35\NAudio.dll + True + diff --git a/SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs b/SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs index b412506439..b54474b09e 100644 --- a/SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs +++ b/SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs @@ -38,34 +38,31 @@ public class CachedAudioDeviceLister : IAudioDeviceLister public CachedAudioDeviceLister(DeviceState state) { _state = state; - Refresh(); MMNotificationClient.Instance.DevicesChanged += DeviceChanged; } private void DeviceChanged(object sender, DeviceChangedEventBase e) { - _dispatcher.Debounce(150, (o) => Refresh()); + _dispatcher.Debounce(150, async (o) => await Refresh()); } - private void Refresh() + public async Task Refresh() { Log.Information("Refreshing device of state {@State}", _state); var playbackTask = Task>.Factory.StartNew((() => { - using (var enumerator = new MMDeviceEnumerator()) - { - return CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Render, _state)); - } + 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)); - } + using var enumerator = new MMDeviceEnumerator(); + return CreateDeviceList(enumerator.EnumerateAudioEndPoints(DataFlow.Capture, _state)); })); - PlaybackDevices = playbackTask.Result; - RecordingDevices = recordingTask.Result; + var results = await Task.WhenAll(playbackTask, recordingTask); + PlaybackDevices = results[0]; + RecordingDevices = results[1]; + Log.Information("Refreshed device of state {@State}", _state); } @@ -82,7 +79,7 @@ private static ICollection CreateDeviceList(MMDeviceCollection c continue; } - sortedDevices.Add(device.FriendlyName, deviceInfo); + sortedDevices.Add(device.ID, deviceInfo); } catch (Exception) { diff --git a/SoundSwitch/Model/IAudioDeviceLister.cs b/SoundSwitch/Model/IAudioDeviceLister.cs index 9b38de1de9..aa49e7183d 100644 --- a/SoundSwitch/Model/IAudioDeviceLister.cs +++ b/SoundSwitch/Model/IAudioDeviceLister.cs @@ -14,6 +14,7 @@ using System; using System.Collections.Generic; +using System.Threading.Tasks; using NAudio.CoreAudioApi; using SoundSwitch.Framework.Audio.Device; @@ -32,5 +33,7 @@ public interface IAudioDeviceLister : IDisposable /// /// ICollection RecordingDevices { get; } + + Task Refresh(); } } \ No newline at end of file diff --git a/SoundSwitch/Program.cs b/SoundSwitch/Program.cs index fc7ad14837..09c6da7557 100644 --- a/SoundSwitch/Program.cs +++ b/SoundSwitch/Program.cs @@ -21,6 +21,7 @@ using System.Runtime.InteropServices; using System.Runtime.Remoting; using System.Threading; +using System.Threading.Tasks; using System.Windows.Forms; using NAudio.CoreAudioApi; using Serilog; @@ -47,19 +48,18 @@ internal static class Program [HandleProcessCorruptedStateExceptions] [STAThread] - private static void Main() + private static async Task Main() { bool createdNew; InitializeLogger(); Log.Information("Application Starts"); - using (var audioDeviceLister = new CachedAudioDeviceLister(DeviceState.Active)) - { - var recordingDevices = audioDeviceLister.RecordingDevices; - Log.Information("Devices Recording {device}", recordingDevices); - var playbackDevices = audioDeviceLister.PlaybackDevices; + using var audioDeviceLister = new CachedAudioDeviceLister(DeviceState.Active); + await audioDeviceLister.Refresh(); + var recordingDevices = audioDeviceLister.RecordingDevices; + Log.Information("Devices Recording {device}", recordingDevices); + var playbackDevices = audioDeviceLister.PlaybackDevices; - Log.Information("Devices Playback {device}", playbackDevices); - } + Log.Information("Devices Playback {device}", playbackDevices); #if !DEBUG AppDomain.CurrentDomain.UnhandledException += (sender, args) => { @@ -97,7 +97,9 @@ private static void Main() } } - AppModel.Instance.ActiveAudioDeviceLister = new CachedAudioDeviceLister(DeviceState.Active); + var deviceActiveLister = new CachedAudioDeviceLister(DeviceState.Active); + await deviceActiveLister.Refresh(); + AppModel.Instance.ActiveAudioDeviceLister = deviceActiveLister; // Windows Vista or newer. if (Environment.OSVersion.Version.Major >= 6) @@ -161,7 +163,7 @@ private static void Main() }; if (AppConfigs.Configuration.FirstRun) { - icon.ShowSettings(); + await icon.ShowSettings(); AppConfigs.Configuration.FirstRun = false; Log.Information("First run"); } diff --git a/SoundSwitch/SoundSwitch.csproj b/SoundSwitch/SoundSwitch.csproj index 25cbf20a58..83cdb15260 100644 --- a/SoundSwitch/SoundSwitch.csproj +++ b/SoundSwitch/SoundSwitch.csproj @@ -229,37 +229,40 @@ ..\packages\Microsoft-WindowsAPICodePack-Core.1.1.3.3\lib\net452\Microsoft.WindowsAPICodePack.dll - - ..\packages\NAudio.1.8.5\lib\net35\NAudio.dll + + ..\packages\NAudio.1.9.0\lib\net35\NAudio.dll True - - ..\packages\Newtonsoft.Json.12.0.1\lib\net45\Newtonsoft.Json.dll + + ..\packages\Newtonsoft.Json.12.0.3\lib\net45\Newtonsoft.Json.dll + True - - ..\packages\Serilog.2.8.0\lib\net46\Serilog.dll + + ..\packages\Serilog.2.9.0\lib\net46\Serilog.dll True ..\packages\Serilog.Enrichers.Environment.2.1.3\lib\net45\Serilog.Enrichers.Environment.dll - - ..\packages\Serilog.Enrichers.Thread.3.0.0\lib\net45\Serilog.Enrichers.Thread.dll + + ..\packages\Serilog.Enrichers.Thread.3.1.0\lib\net45\Serilog.Enrichers.Thread.dll True - - ..\packages\Serilog.Exceptions.5.0.0\lib\net46\Serilog.Exceptions.dll + + ..\packages\Serilog.Exceptions.5.3.1\lib\net472\Serilog.Exceptions.dll + True - - ..\packages\Serilog.Formatting.Compact.1.0.0\lib\net45\Serilog.Formatting.Compact.dll + + ..\packages\Serilog.Formatting.Compact.1.1.0\lib\net452\Serilog.Formatting.Compact.dll True - - ..\packages\Serilog.Sinks.File.4.0.0\lib\net45\Serilog.Sinks.File.dll + + ..\packages\Serilog.Sinks.File.4.1.0\lib\net45\Serilog.Sinks.File.dll True + @@ -537,8 +540,6 @@ - - @@ -552,7 +553,6 @@ - diff --git a/SoundSwitch/UI/Forms/Settings.cs b/SoundSwitch/UI/Forms/Settings.cs index e02ab481be..f3ab53643a 100644 --- a/SoundSwitch/UI/Forms/Settings.cs +++ b/SoundSwitch/UI/Forms/Settings.cs @@ -18,6 +18,7 @@ using System.ComponentModel; using System.Drawing; using System.Linq; +using System.Threading.Tasks; using System.Windows.Forms; using NAudio.CoreAudioApi; using SoundSwitch.Framework; @@ -41,7 +42,7 @@ public sealed partial class SettingsForm : Form { private static readonly Icon RessourceSettingsIcon = Resources.SettingsIcon; - private readonly bool _loaded; + private bool _loaded; public SettingsForm() { @@ -154,15 +155,18 @@ public SettingsForm() // Settings - Language languageComboBox.Items.AddRange(Enum.GetNames(typeof(Language))); languageComboBox.SelectedIndex = (int) AppConfigs.Configuration.Language; + } + public async Task PopulateAudioDevices() + { + // Playback and Recording - using (var audioDeviceLister = new CachedAudioDeviceLister(DeviceState.All)) - { - PopulateAudioList(playbackListView, AppModel.Instance.SelectedDevices, - audioDeviceLister.PlaybackDevices); - PopulateAudioList(recordingListView, AppModel.Instance.SelectedDevices, - audioDeviceLister.RecordingDevices); - } + using var audioDeviceLister = new CachedAudioDeviceLister(DeviceState.All); + await audioDeviceLister.Refresh(); + PopulateAudioList(playbackListView, AppModel.Instance.SelectedDevices, + audioDeviceLister.PlaybackDevices); + PopulateAudioList(recordingListView, AppModel.Instance.SelectedDevices, + audioDeviceLister.RecordingDevices); _loaded = true; } diff --git a/SoundSwitch/Util/TrayIcon.cs b/SoundSwitch/Util/TrayIcon.cs index 7ea9868e1b..e202b7bd3b 100644 --- a/SoundSwitch/Util/TrayIcon.cs +++ b/SoundSwitch/Util/TrayIcon.cs @@ -20,6 +20,7 @@ using System.Linq; using System.Reflection; using System.Threading; +using System.Threading.Tasks; using System.Windows.Forms; using NAudio.CoreAudioApi; using Serilog; @@ -181,7 +182,7 @@ private void PopulateSettingsMenu() _settingsMenu.Items.Add("-"); _settingsMenu.Items.Add(_updateMenuItem); _settingsMenu.Items.Add(TrayIconStrings.settings, RessourceSettingsSmallBitmap, - (sender, e) => ShowSettings()); + async (sender, e) => await ShowSettings()); _settingsMenu.Items.Add("-"); _settingsMenu.Items.Add(TrayIconStrings.help, RessourceInfoHelpBitmap, (sender, e) => { @@ -294,9 +295,11 @@ private void StopAnimationIconUpdate() } - public void ShowSettings() + public async Task ShowSettings() { - new SettingsForm().Show(); + var settingsForm = new SettingsForm(); + await settingsForm.PopulateAudioDevices(); + settingsForm.Show(); } ///