Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creates lots of Tasks that don't go away #74

Open
mikemeinz opened this issue Jun 4, 2024 · 4 comments
Open

Creates lots of Tasks that don't go away #74

mikemeinz opened this issue Jun 4, 2024 · 4 comments

Comments

@mikemeinz
Copy link

mikemeinz commented Jun 4, 2024

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.

@xenolightning
Copy link
Owner

Hmmm, that's a little weird.

Can you post a sample of the code so I can take a look?

@mikemeinz
Copy link
Author

.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
}
}
}

@xenolightning
Copy link
Owner

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

@mikemeinz
Copy link
Author

mikemeinz commented Jun 4, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants