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
+45 -19
+45 -19
View File
@@ -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
{
@@ -256,10 +257,17 @@ namespace Vocaluxe.Base
{
if (_PreviewStartHelperTask != null && ( _PreviewPlayer.Length > 0 || _PreviewStartWaitCounter++ >= _PreviewStartWaitMaxTries))
{
_PreviewStartHelperTask.RunSynchronously(TaskScheduler.FromCurrentSynchronizationContext());
_PreviewStartHelperTask.Dispose();
_PreviewStartHelperTask = null;
_PreviewStartWaitCounter = 0;
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)
@@ -317,28 +325,46 @@ namespace Vocaluxe.Base
//Change song position only if song is changed or near to end
if (songChanged || _CurPlayer.Position + 30 < _CurPlayer.Length)
{
_PreviewStartHelperTask = new Task(() =>
lock (_PreviewStartHelperTaskLock)
{
float length = _PreviewPlayer.Length;
if (length < 1)
length = 30; // If length is unknow or invalid assume a length of 30s
// Check if the old _PreviewStartHelperTask needs to disposed
if (_PreviewStartHelperTask != null)
{
_PreviewStartHelperTask.Dispose();
}
_PreviewStartHelperTask = new Task(() =>
{
float length = _PreviewPlayer.Length;
if (length < 1)
length = 30; // If length is unknow or invalid assume a length of 30s
if (start < 0)
start = (song.Preview.Source == EDataSource.None) ? length / 4f : song.Preview.StartTime;
if (start > length - 5f)
start = Math.Max(0f, Math.Min(length / 4f, length - 5f));
if (start >= 0.5f)
start -= 0.5f;
if (start < 0)
start = (song.Preview.Source == EDataSource.None) ? length / 4f : song.Preview.StartTime;
if (start > length - 5f)
start = Math.Max(0f, Math.Min(length / 4f, length - 5f));
if (start >= 0.5f)
start -= 0.5f;
_PreviewPlayer.Position = start;
Play();
});
_PreviewPlayer.Position = start;
Play();
});
}
_CurPlayer = _PreviewPlayer;
}
else
{
_PreviewStartHelperTask.Dispose();
_PreviewStartHelperTask = null;
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();