Synchronize access to _PreviewStartHelperTask object (may fixes #330)
This commit is contained in:
1 parent
ceaa77ffe7
commit
3e0eec8294
1 file changed
+45
-19
@@ -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
|
||||||
{
|
{
|
||||||
@@ -256,10 +257,17 @@ namespace Vocaluxe.Base
|
|||||||
{
|
{
|
||||||
if (_PreviewStartHelperTask != null && ( _PreviewPlayer.Length > 0 || _PreviewStartWaitCounter++ >= _PreviewStartWaitMaxTries))
|
if (_PreviewStartHelperTask != null && ( _PreviewPlayer.Length > 0 || _PreviewStartWaitCounter++ >= _PreviewStartWaitMaxTries))
|
||||||
{
|
{
|
||||||
_PreviewStartHelperTask.RunSynchronously(TaskScheduler.FromCurrentSynchronizationContext());
|
lock (_PreviewStartHelperTaskLock)
|
||||||
_PreviewStartHelperTask.Dispose();
|
{
|
||||||
_PreviewStartHelperTask = null;
|
// Recheck the condition as it cloud have change before we got the lock
|
||||||
_PreviewStartWaitCounter = 0;
|
if (_PreviewStartHelperTask != null && (_PreviewPlayer.Length > 0 || _PreviewStartWaitCounter++ >= _PreviewStartWaitMaxTries))
|
||||||
|
{
|
||||||
|
_PreviewStartHelperTask.RunSynchronously(TaskScheduler.FromCurrentSynchronizationContext());
|
||||||
|
_PreviewStartHelperTask.Dispose();
|
||||||
|
_PreviewStartHelperTask = null;
|
||||||
|
_PreviewStartWaitCounter = 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!IsPlaying)
|
if (!IsPlaying)
|
||||||
@@ -317,28 +325,46 @@ 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)
|
||||||
{
|
{
|
||||||
_PreviewStartHelperTask = new Task(() =>
|
lock (_PreviewStartHelperTaskLock)
|
||||||
{
|
{
|
||||||
float length = _PreviewPlayer.Length;
|
// Check if the old _PreviewStartHelperTask needs to disposed
|
||||||
if (length < 1)
|
if (_PreviewStartHelperTask != null)
|
||||||
length = 30; // If length is unknow or invalid assume a length of 30s
|
{
|
||||||
|
_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)
|
if (start < 0)
|
||||||
start = (song.Preview.Source == EDataSource.None) ? length / 4f : song.Preview.StartTime;
|
start = (song.Preview.Source == EDataSource.None) ? length / 4f : song.Preview.StartTime;
|
||||||
if (start > length - 5f)
|
if (start > length - 5f)
|
||||||
start = Math.Max(0f, Math.Min(length / 4f, length - 5f));
|
start = Math.Max(0f, Math.Min(length / 4f, length - 5f));
|
||||||
if (start >= 0.5f)
|
if (start >= 0.5f)
|
||||||
start -= 0.5f;
|
start -= 0.5f;
|
||||||
|
|
||||||
_PreviewPlayer.Position = start;
|
_PreviewPlayer.Position = start;
|
||||||
Play();
|
Play();
|
||||||
});
|
});
|
||||||
|
}
|
||||||
_CurPlayer = _PreviewPlayer;
|
_CurPlayer = _PreviewPlayer;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_PreviewStartHelperTask.Dispose();
|
if (_PreviewStartHelperTask != null)
|
||||||
_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;
|
_PreviewPlayer.Position = _CurPlayer.Position;
|
||||||
_CurPlayer = _PreviewPlayer;
|
_CurPlayer = _PreviewPlayer;
|
||||||
Play();
|
Play();
|
||||||
|
|||||||
Reference in new issue
Block a user