AudioSwitcher
AudioSwitcher copied to clipboard
Creates lots of Tasks that don't go away
I suspect that this NuGet package would be OK to use if you need to make a few calls. If you have a long running program, know that this NuGet package creates tasks(threads) and doesn't seem to terminate them.
In my case, I have a program that runs continuously to monitor an audio interface device to be sure it is the default playback device, that the volume is 100.0 and that it is not muted. After less than 24 hours, the PC locked up. I tried in Debug mode and found when I paused my app that Visual Studio reported lots and lots of tasks(threads) awaiting. Yes, I am using the latest 4.0 pre-release version created in 2017.
I have removed AudioSwitcher from my program and I am reverting to periodic human checking of the audio interface for the time being.
Hmmm, that's a little weird.
Can you post a sample of the code so I can take a look?
.NET Framework 4.8.1 4.0.0-Alpha5
To test, I had a slightly modified version of this running at the same time as this version was running. That one changed the volume and muted the audio every 3 seconds. Both in separate instances of Visual Studio 2022 and both run in DEBUG mode.
#define CHECKDEVICE #define CONTINUOUS using System; using System.Collections; using System.Diagnostics; using System.Text; using AudioSwitcher; using AudioSwitcher.AudioApi; using AudioSwitcher.AudioApi.CoreAudio; #if CONTINUOUS using System.Threading; #endif
namespace SetSpeaker
{
internal class Program
{
static void Main(string[] args)
{
#if CONTINUOUS
do
{
#endif
Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fffff"));
const double StandardAudioVolume = 100.0f; // Max volume
#if CHECKDEVICE
const string TARGETOUTPUT = "Crestron";
CoreAudioDevice itemTarget = null;
CoreAudioDevice itemCurrentDefault = null;
#endif
try
{
#if CHECKDEVICE
var devices = new CoreAudioController().GetPlaybackDevices(); // Get all playback devices
IEnumerator enumerator = devices.GetEnumerator();
while (enumerator.MoveNext())
{
CoreAudioDevice currDev = (CoreAudioDevice)enumerator.Current; // Get current indexed device
Debug.WriteLine($"FullName={currDev.FullName}, Default={currDev.IsDefaultDevice}");
//Console.WriteLine($"FullName={currDev.FullName}, Default={currDev.IsDefaultDevice}");
if (currDev.FullName.Contains("Speaker") || currDev.FullName.Contains("Display Audio"))
{
if (currDev.IsDefaultDevice)
{
// Save the address of the Default playback device
itemCurrentDefault = currDev;
}
if (currDev.FullName.Contains(TARGETOUTPUT))
{
// Save the address of the Target playback device
itemTarget = currDev;
}
}
}
if (itemTarget is null)
{
// If the Target playback device was not found, don't chnage the default playback device
}
else
{
// If the Target sound device is already the default, do nothing.
if (!ReferenceEquals(itemTarget, itemCurrentDefault))
{
itemTarget.SetAsDefault();
Debug.WriteLine($"{itemTarget.FullName} Set As Default");
//Console.WriteLine($"{itemTarget.FullName} Set As Default");
}
}
if (itemTarget is null)
{
// If the Target playback device was not found, don't change volume settings
}
else
{
#endif CoreAudioDevice defaultDevice = new CoreAudioController().DefaultPlaybackDevice; if (defaultDevice.Volume != StandardAudioVolume) { defaultDevice.SetVolumeAsync(StandardAudioVolume); } if (defaultDevice.IsMuted) { defaultDevice.ToggleMuteAsync(); } #if CHECKDEVICE if (itemTarget.Volume != StandardAudioVolume) { itemTarget.SetVolumeAsync(StandardAudioVolume); } if (itemTarget.IsMuted) { itemTarget.ToggleMuteAsync(); } } #endif } catch (Exception ex) { Debug.WriteLine(ex.Message); } Debug.WriteLine(DateTime.Now.ToString("hh:mm:ss.fffff")); #if CONTINUOUS Thread.Sleep(4500); } while (true); #endif } } }
You are creating a CoreAudioController controller many, many times.
This should only be created once per application (ideally, but a few times here and there is also fine)
It should also be disposed after use
I'd move the creation of the controller outside of your main loop, it should solve the task issue
Thanks.I didn't see that in any of the examples I found.Sent from my Galaxy -------- Original message --------From: Sean Chapman @.> Date: 6/4/24 5:41 PM (GMT-05:00) To: xenolightning/AudioSwitcher @.> Cc: Mike Meinz @.>, Author @.> Subject: Re: [xenolightning/AudioSwitcher] Creates lots of Tasks that don't go away (Issue #74) You are creating a CoreAudioController controller many, many times. This should only be created once per application (ideally, but a few times here and there is also fine) It should also be disposed after use I'd move the creation of the controller outside of your main loop, it should solve the task issue
—Reply to this email directly, view it on GitHub, or unsubscribe.You are receiving this because you authored the thread.Message ID: @.***>