使用dispatcher.BeginInvoke时,何时/为什么我需要调用Dispatcher.Run?

时间:2011-06-22 01:26:10

标签: c# wpf dispatcher

另外,如果我将this.dispatcher与另一个BeginInvoke重用,我是否还需要“Dispatcher.Run”?

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

1 个答案:

答案 0 :(得分:3)

Dispatcher.BeginInvoke将一个委托添加到调度程序的事件队列中。

队列仅在Run()内处理。

Run()是一个永久执行的阻止调用(或直到您调用InvokeShutdown())。

如果再次致电BeginInvoke()Run()会立即看到新代表并立即执行。 (或者一旦它是免费的)

相关问题