Reputation: 15047
Right, I am programming something for a GUI at this moment. I am however facing a synchronization problem that I can't seem to fix myself.
I am starting/stopping the playback of a video remotely through UDP. The incoming code/command is parsed and then executed in the 'Window' class.
Since it is a GUI element any calls going to an element has to be executed on the thread that it was created from. Though, my UDP client is run on its own thread. Thus any calls need to go through the Dispatcher.Invoke method.
That is working fine, however. There are multiple problems with this. For example the 'load' method and 'play' method can be executed directly after each other. Which can cause the 'play' method to be executed before the 'load' method. Because I can't control when an action is invoked on said object.
So I thought, I'll use a monitor/mutex/semaphore/lock() on the object and then wait before it is released a again. See below code snippets. But this is causing an exception:
SynchronizationLockException is thrown by calling the Exit, Pulse, PulseAll, and Wait methods of the Monitor class from an unsynchronized block of code.
So I am looking for a better/working way to synchronize both threads. That if the call is invoked through the dispatcher, the other thread will basicly wait until the invoked method is done invoking.
Also, excuse my wording if I am using wrong terms.
Dispatch method:
#region ExecuteDispatch Members
public void Dispatch(Callback call)
{
Dispatch(this, call);
}
public void Dispatch(DispatcherObject o, Callback call)
{
if (!o.Dispatcher.CheckAccess())
{
o.Dispatcher.Invoke(
System.Windows.Threading.DispatcherPriority.Send,
new Action(call)
);
}
else
{
call();
}
}
#endregion
Method that should be 'synchronized':
Monitor.Enter(player);
Dispatch(delegate()
{
player.Stop();
Monitor.Exit(player);
});
Monitor.Enter(player);
Monitor.Exit(player);
Upvotes: 2
Views: 1007
Reputation: 11760
One possible way is to have one thread signal the other that he finished his work. Therefore you can use the AutoResetEvent or the ManualResetEvent:
AutoResetEvent evt = new AutoResetEvent(false);
Dispatch(delegate()
{
player.Stop();
evt.Set();
});
evt.WaitOne(); //Here the thread waits until Set is called on evt
Upvotes: 2