Synchronize access to _PreviewStartHelperTask object (may fixes #330)

This commit is contained in:
lukeIam committed 2017-11-04 14:58:48 +01:00
1 parent ceaa77ffe7
commit 3e0eec8294
1 file changed
+26
+26
View File
@@ -51,6 +51,7 @@ namespace Vocaluxe.Base
private static Task _PreviewStartHelperTask; private static Task _PreviewStartHelperTask;
private static int _PreviewStartWaitCounter; private static int _PreviewStartWaitCounter;
private const int _PreviewStartWaitMaxTries = 10; private const int _PreviewStartWaitMaxTries = 10;
private static readonly object _PreviewStartHelperTaskLock = new Object();
public static bool VideoEnabled public static bool VideoEnabled
{ {
@@ -255,12 +256,19 @@ namespace Vocaluxe.Base
public static void Update() public static void Update()
{ {
if (_PreviewStartHelperTask != null && ( _PreviewPlayer.Length > 0 || _PreviewStartWaitCounter++ >= _PreviewStartWaitMaxTries)) if (_PreviewStartHelperTask != null && ( _PreviewPlayer.Length > 0 || _PreviewStartWaitCounter++ >= _PreviewStartWaitMaxTries))
{
lock (_PreviewStartHelperTaskLock)
{
// Recheck the condition as it cloud have change before we got the lock
if (_PreviewStartHelperTask != null && (_PreviewPlayer.Length > 0 || _PreviewStartWaitCounter++ >= _PreviewStartWaitMaxTries))
{ {
_PreviewStartHelperTask.RunSynchronously(TaskScheduler.FromCurrentSynchronizationContext()); _PreviewStartHelperTask.RunSynchronously(TaskScheduler.FromCurrentSynchronizationContext());
_PreviewStartHelperTask.Dispose(); _PreviewStartHelperTask.Dispose();
_PreviewStartHelperTask = null; _PreviewStartHelperTask = null;
_PreviewStartWaitCounter = 0; _PreviewStartWaitCounter = 0;
} }
}
}
if (!IsPlaying) if (!IsPlaying)
return; return;
@@ -317,6 +325,13 @@ namespace Vocaluxe.Base
//Change song position only if song is changed or near to end //Change song position only if song is changed or near to end
if (songChanged || _CurPlayer.Position + 30 < _CurPlayer.Length) if (songChanged || _CurPlayer.Position + 30 < _CurPlayer.Length)
{ {
lock (_PreviewStartHelperTaskLock)
{
// Check if the old _PreviewStartHelperTask needs to disposed
if (_PreviewStartHelperTask != null)
{
_PreviewStartHelperTask.Dispose();
}
_PreviewStartHelperTask = new Task(() => _PreviewStartHelperTask = new Task(() =>
{ {
float length = _PreviewPlayer.Length; float length = _PreviewPlayer.Length;
@@ -333,12 +348,23 @@ namespace Vocaluxe.Base
_PreviewPlayer.Position = start; _PreviewPlayer.Position = start;
Play(); Play();
}); });
}
_CurPlayer = _PreviewPlayer; _CurPlayer = _PreviewPlayer;
} }
else else
{
if (_PreviewStartHelperTask != null)
{
lock (_PreviewStartHelperTaskLock)
{
// Recheck the condition as it cloud have change before we got the lock
if (_PreviewStartHelperTask != null)
{ {
_PreviewStartHelperTask.Dispose(); _PreviewStartHelperTask.Dispose();
_PreviewStartHelperTask = null; _PreviewStartHelperTask = null;
}
}
}
_PreviewPlayer.Position = _CurPlayer.Position; _PreviewPlayer.Position = _CurPlayer.Position;
_CurPlayer = _PreviewPlayer; _CurPlayer = _PreviewPlayer;
Play(); Play();