Skip to content

Commit dbe1061

Browse files
author
Antoine Aflalo
committed
Possible fixe for RDP problems
Crashing when launching the app because of problem with the COM thread. Fixes #187
1 parent 0ba46cf commit dbe1061

File tree

2 files changed

+20
-9
lines changed

2 files changed

+20
-9
lines changed

SoundSwitch/Framework/NotificationManager/Notification/NotificationCustom.cs

+10-2
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,28 @@ namespace SoundSwitch.Framework.NotificationManager.Notification
2525
{
2626
public class NotificationCustom : INotification
2727
{
28-
private readonly MMDeviceEnumerator _deviceEnumerator = new MMDeviceEnumerator();
28+
private MMDeviceEnumerator _deviceEnumerator;
2929

3030
public NotificationTypeEnum TypeEnum => NotificationTypeEnum.CustomNotification;
3131
public string Label => SettingsStrings.notificationOptionCustomized;
3232

3333
public INotificationConfiguration Configuration { get; set; }
3434

35+
private MMDeviceEnumerator GetEnumerator()
36+
{
37+
if (_deviceEnumerator != null)
38+
return _deviceEnumerator;
39+
40+
return _deviceEnumerator = new MMDeviceEnumerator();
41+
}
42+
3543
public void NotifyDefaultChanged(IAudioDevice audioDevice)
3644
{
3745
if (audioDevice.Type != AudioDeviceType.Playback)
3846
return;
3947
var task = new Task(() =>
4048
{
41-
var device = _deviceEnumerator.GetDevice(audioDevice.Id);
49+
var device = GetEnumerator().GetDevice(audioDevice.Id);
4250
using (var output = new WasapiOut(device, AudioClientShareMode.Shared, true, 10))
4351
using (var waveStream = new CachedSoundWaveStream(Configuration.CustomSound))
4452
{

SoundSwitch/Framework/NotificationManager/Notification/NotificationSound.cs

+10-7
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,21 @@ namespace SoundSwitch.Framework.NotificationManager.Notification
2626
{
2727
public class NotificationSound : INotification
2828
{
29-
private readonly MMDeviceEnumerator _deviceEnumerator;
30-
31-
public NotificationSound()
32-
{
33-
_deviceEnumerator = new MMDeviceEnumerator();
34-
}
29+
private MMDeviceEnumerator _deviceEnumerator;
3530

3631
public NotificationTypeEnum TypeEnum => NotificationTypeEnum.SoundNotification;
3732
public string Label => SettingsStrings.notificationOptionSound;
3833

3934
public INotificationConfiguration Configuration { get; set; }
4035

36+
private MMDeviceEnumerator GetEnumerator()
37+
{
38+
if (_deviceEnumerator != null)
39+
return _deviceEnumerator;
40+
41+
return _deviceEnumerator = new MMDeviceEnumerator();
42+
}
43+
4144
public void NotifyDefaultChanged(IAudioDevice audioDevice)
4245
{
4346
if (audioDevice.Type != AudioDeviceType.Playback)
@@ -47,7 +50,7 @@ public void NotifyDefaultChanged(IAudioDevice audioDevice)
4750
{
4851
using (var memoryStreamedSound = GetStreamCopy())
4952
{
50-
var device = _deviceEnumerator.GetDevice(audioDevice.Id);
53+
var device = GetEnumerator().GetDevice(audioDevice.Id);
5154
using (var output = new WasapiOut(device, AudioClientShareMode.Shared, true, 10))
5255
{
5356
output.Init(new WaveFileReader(memoryStreamedSound));

0 commit comments

Comments
 (0)