I have the same problem as this person had. However the question was answered by using .Net 4.5, but I only have .Net 4.0.
So I created my own asynchronous semaphore based on this tutorial and implemented my code based on additional comments of the guy that posted the anwer:
private void Foo()
{
try
{
Semaphore.WaitAsync().ContinueWith(previousTask =>
{
if (Dispatcher.FromThread(Thread.CurrentThread) != null)
{
Bar();
}
else
{
Application.Current.Dispatcher.Invoke(new Action(() => Bar()));
}
});
}
finally
{
Semaphore.Release();
}
}
This does not work for me though, Bar is called in parallel.