簡體   English   中英

使用dispatcher.BeginInvoke時,何時/為什么需要調用Dispatcher.Run?

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

另外,如果我將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();

Dispatcher.BeginInvoke將委托添加到調度程序的事件隊列中。

僅在Run()內部處理隊列。

Run()是一個阻塞調用,它將永遠執行(或者直到您調用InvokeShutdown()為止)。

如果再次調用BeginInvoke()Run()將立即看到新的委托並立即執行。 (或免費)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM