Synchronize access to _PreviewStartHelperTask object (may fixes #330)
This commit is contained in:
1 parent
ceaa77ffe7
commit
3e0eec8294
1 file changed
+26
@@ -51,6 +51,7 @@ namespace Vocaluxe.Base
|
||||
private static Task _PreviewStartHelperTask;
|
||||
private static int _PreviewStartWaitCounter;
|
||||
private const int _PreviewStartWaitMaxTries = 10;
|
||||
private static readonly object _PreviewStartHelperTaskLock = new Object();
|
||||
|
||||
public static bool VideoEnabled
|
||||
{
|
||||
@@ -255,12 +256,19 @@ namespace Vocaluxe.Base
|
||||
public static void Update()
|
||||
{
|
||||
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.Dispose();
|
||||
_PreviewStartHelperTask = null;
|
||||
_PreviewStartWaitCounter = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!IsPlaying)
|
||||
return;
|
||||
@@ -317,6 +325,13 @@ namespace Vocaluxe.Base
|
||||
//Change song position only if song is changed or near to end
|
||||
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(() =>
|
||||
{
|
||||
float length = _PreviewPlayer.Length;
|
||||
@@ -333,12 +348,23 @@ namespace Vocaluxe.Base
|
||||
_PreviewPlayer.Position = start;
|
||||
Play();
|
||||
});
|
||||
}
|
||||
_CurPlayer = _PreviewPlayer;
|
||||
}
|
||||
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 = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
_PreviewPlayer.Position = _CurPlayer.Position;
|
||||
_CurPlayer = _PreviewPlayer;
|
||||
Play();
|
||||
|
||||
Reference in new issue
Block a user