Also, if I reuse this.dispatcher with another BeginInvoke, do I need “Dispatcher.Run” again?
var thread = new Thread(() =>
{
this.dispatcher = Dispatcher.CurrentDispatcher;
this.dispatcher .BeginInvoke(new Action(() =>
{
try
{
do something
}
catch (Exception ex)
{
onNotify(ex);
}
}));
Dispatcher.Run();
});
thread.Name = string.Format("{0} Hook Thread", this.GetType().Name);
thread.IsBackground = true;
thread.SetApartmentState(ApartmentState.STA);
thread.Start();
Dispatcher.BeginInvokeadds a delegate on to the dispatcher’s event queue.The queue is only processed inside
Run().Run()is a blocking call that will execute forever (or until you callInvokeShutdown()).If you call
BeginInvoke()again,Run()will see the new delegate right away and execute it immediately. (Or as soon as it’s free)