Skip to content

Commit d56d85b

Browse files
committed
Add lock for sound & Rename variable
1 parent 5b6ea73 commit d56d85b

File tree

1 file changed

+30
-22
lines changed

1 file changed

+30
-22
lines changed

Flow.Launcher/MainWindow.xaml.cs

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.ComponentModel;
33
using System.Linq;
44
using System.Media;
5+
using System.Threading;
56
using System.Threading.Tasks;
67
using System.Windows;
78
using System.Windows.Controls;
@@ -61,8 +62,9 @@ public partial class MainWindow : IDisposable
6162
private bool _isArrowKeyPressed = false;
6263

6364
// Window Sound Effects
64-
private MediaPlayer animationSoundWMP;
65-
private SoundPlayer animationSoundWPF;
65+
private MediaPlayer _animationSoundWMP;
66+
private SoundPlayer _animationSoundWPF;
67+
private readonly Lock _soundLock = new();
6668

6769
// Window WndProc
6870
private HwndSource _hwndSource;
@@ -687,31 +689,37 @@ private IntPtr WndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref b
687689

688690
private void InitSoundEffects()
689691
{
690-
if (_settings.WMPInstalled)
692+
lock (_soundLock)
691693
{
692-
animationSoundWMP?.Close();
693-
animationSoundWMP = new MediaPlayer();
694-
animationSoundWMP.Open(new Uri(AppContext.BaseDirectory + "Resources\\open.wav"));
695-
}
696-
else
697-
{
698-
animationSoundWPF?.Dispose();
699-
animationSoundWPF = new SoundPlayer(AppContext.BaseDirectory + "Resources\\open.wav");
700-
animationSoundWPF.Load();
694+
if (_settings.WMPInstalled)
695+
{
696+
_animationSoundWMP?.Close();
697+
_animationSoundWMP = new MediaPlayer();
698+
_animationSoundWMP.Open(new Uri(AppContext.BaseDirectory + "Resources\\open.wav"));
699+
}
700+
else
701+
{
702+
_animationSoundWPF?.Dispose();
703+
_animationSoundWPF = new SoundPlayer(AppContext.BaseDirectory + "Resources\\open.wav");
704+
_animationSoundWPF.Load();
705+
}
701706
}
702707
}
703708

704709
private void SoundPlay()
705710
{
706-
if (_settings.WMPInstalled)
711+
lock (_soundLock)
707712
{
708-
animationSoundWMP.Position = TimeSpan.Zero;
709-
animationSoundWMP.Volume = _settings.SoundVolume / 100.0;
710-
animationSoundWMP.Play();
711-
}
712-
else
713-
{
714-
animationSoundWPF.Play();
713+
if (_settings.WMPInstalled)
714+
{
715+
_animationSoundWMP.Position = TimeSpan.Zero;
716+
_animationSoundWMP.Volume = _settings.SoundVolume / 100.0;
717+
_animationSoundWMP.Play();
718+
}
719+
else
720+
{
721+
_animationSoundWPF.Play();
722+
}
715723
}
716724
}
717725

@@ -1436,8 +1444,8 @@ protected virtual void Dispose(bool disposing)
14361444
{
14371445
_hwndSource?.Dispose();
14381446
_notifyIcon?.Dispose();
1439-
animationSoundWMP?.Close();
1440-
animationSoundWPF?.Dispose();
1447+
_animationSoundWMP?.Close();
1448+
_animationSoundWPF?.Dispose();
14411449
_viewModel.ActualApplicationThemeChanged -= ViewModel_ActualApplicationThemeChanged;
14421450
}
14431451

0 commit comments

Comments
 (0)