tofutim
tofutim

Reputation: 23374

When using dispatcher.BeginInvoke, when/why do I need to call Dispatcher.Run?

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();

Upvotes: 0

Views: 1489

Answers (1)

SLaks
SLaks

Reputation: 887195

Dispatcher.BeginInvoke adds 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 call InvokeShutdown()).

If you call BeginInvoke() again, Run() will see the new delegate right away and execute it immediately. (Or as soon as it's free)

Upvotes: 3

Related Questions