Skip to content

Commit

Permalink
This fixes a race condition when creating multiple VLC controls. Prev…
Browse files Browse the repository at this point in the history
…iously two different controls could get different versions of the CommonDispatcher if one tried to access it while the other was in the while loop, which would eventually lead to one of the controls getting the wrong Dispatcher and thereby crash when accessing ImageSource later.
  • Loading branch information
dennisgranasen committed May 28, 2018
1 parent 071ed1f commit e28590c
Showing 1 changed file with 20 additions and 17 deletions.
37 changes: 20 additions & 17 deletions Meta.Vlc.Wpf/ThreadSeparatedImage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,34 @@ namespace Meta.Vlc.Wpf
public sealed class ThreadSeparatedImage : ThreadSeparatedControlHost
{
private static Dispatcher _commonDispatcher;
private static object _staticLock = new object();
public static Dispatcher CommonDispatcher
{
get
{
if (_commonDispatcher == null)
lock (_staticLock)
{
Thread separateThread = new Thread(() =>
if (_commonDispatcher == null)
{
Dispatcher.Run();
})
{
IsBackground = true
};
separateThread.SetApartmentState(ApartmentState.STA);
separateThread.Priority = ThreadPriority.Highest;

separateThread.Start();

while (Dispatcher.FromThread(separateThread) == null)
{
Thread.Sleep(50);
Thread separateThread = new Thread(() =>
{
Dispatcher.Run();
})
{
IsBackground = true
};
separateThread.SetApartmentState(ApartmentState.STA);
separateThread.Priority = ThreadPriority.Highest;

separateThread.Start();

while (Dispatcher.FromThread(separateThread) == null)
{
Thread.Sleep(50);
}
_commonDispatcher = Dispatcher.FromThread(separateThread);
}
_commonDispatcher = Dispatcher.FromThread(separateThread);
}

return _commonDispatcher;
}
}
Expand Down

0 comments on commit e28590c

Please sign in to comment.