![](/img/trans.png)
[英]Why do I get “Invalid cross-thread access” when using Dispatcher.BeginInvoke()?
[英]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.