diff --git a/SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs b/SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs index 32388c84a7..4115934821 100644 --- a/SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs +++ b/SoundSwitch/Framework/Audio/Lister/CachedAudioDeviceLister.cs @@ -1,16 +1,16 @@ /******************************************************************** -* Copyright (C) 2015-2017 Antoine Aflalo -* -* This program is free software; you can redistribute it and/or -* modify it under the terms of the GNU General Public License -* as published by the Free Software Foundation; either version 2 -* of the License, or (at your option) any later version. -* -* This program is distributed in the hope that it will be useful, -* but WITHOUT ANY WARRANTY; without even the implied warranty of -* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -* GNU General Public License for more details. -********************************************************************/ + * Copyright (C) 2015-2017 Antoine Aflalo + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + ********************************************************************/ using System; using System.Collections.Generic; @@ -32,10 +32,10 @@ namespace SoundSwitch.Framework.Audio.Lister public class CachedAudioDeviceLister : IAudioDeviceLister { /// - private DeviceReadOnlyCollection PlaybackDevices { get; set; } = new(Enumerable.Empty(), DataFlow.Render); + private DeviceReadOnlyCollection PlaybackDevices { get; set; } = new(Enumerable.Empty(), DataFlow.Render); /// - private DeviceReadOnlyCollection RecordingDevices { get; set; } = new(Enumerable.Empty(), DataFlow.Capture); + private DeviceReadOnlyCollection RecordingDevices { get; set; } = new(Enumerable.Empty(), DataFlow.Capture); /// /// Get devices per type and state @@ -58,6 +58,7 @@ public DeviceReadOnlyCollection GetDevices(DataFlow type, Device private readonly ILogger _context; private uint _threadSafeRefreshing; private DateTime _lastRefresh = DateTime.UtcNow; + public bool Refreshing { get => Interlocked.CompareExchange(ref _threadSafeRefreshing, 1, 1) == 1; @@ -94,8 +95,10 @@ public void Refresh(CancellationToken cancellationToken = default) _context.Warning("Can't refresh, already refreshing since {LastRefresh}", _lastRefresh); //We want to be sure we get the latest refresh, it's not because we are refreshing that we'll get the latest info JobScheduler.Instance.ScheduleJob(new DebounceRefreshJob(_state, this, _context)); + return; } + var stopWatch = Stopwatch.StartNew(); try { @@ -151,7 +154,7 @@ public void Refresh(CancellationToken cancellationToken = default) RecordingDevices = new DeviceReadOnlyCollection(recordingDevices.Values, DataFlow.Capture); - _context.Information("Refreshed all devices in {@StopTime}. {@Recording}/rec, {@Playback}/play", stopWatch.Elapsed,recordingDevices.Count, playbackDevices.Count); + _context.Information("Refreshed all devices in {@StopTime}. {@Recording}/rec, {@Playback}/play", stopWatch.Elapsed, recordingDevices.Count, playbackDevices.Count); } //If cancellation token is cancelled, its expected to throw null since the device enumerator has been disposed catch (NullReferenceException e) when (!cancellationToken.IsCancellationRequested) @@ -169,7 +172,7 @@ public void Refresh(CancellationToken cancellationToken = default) public void Dispose() { MMNotificationClient.Instance.DevicesChanged -= DeviceChanged; - + foreach (var device in PlaybackDevices.Union(RecordingDevices)) { device.Dispose();