創建我有下面的代碼,這在它自己的專用UI線程上運行一個WPF窗口:WPF設置所有者的窗口,在自己的專用UI線程
// Create the dedicated UI thread for AddEditPair window
Thread addEditPairThread = new Thread(() =>
{
// Initialise the add edit pair window
addEditPair = new AddEditPair(this);
addEditPair.PairRecordAdded += new EventHandler<PairRecordEventArgs>(addEditPair_PairRecordAdded);
addEditPair.PairRecordEdited += new EventHandler<PairRecordEventArgs>(addEditPair_PairRecordEdited);
// Force AddEditPair to run on own UI thread
System.Windows.Threading.Dispatcher.Run();
});
addEditPairThread.IsBackground = true;
addEditPairThread.Name = "AddEditPair";
addEditPairThread.SetApartmentState(ApartmentState.STA);
addEditPairThread.Start();
這只是當我嘗試設置所有者的偉大工程的窗口,運行在主Ui線程上的窗口。
的例外,我得到的是:
The calling thread cannot access this object because a different thread owns it.
我明白什麼錯誤意味着以及爲什麼會發生,所以後來我實現了以下內容:
// If invoke is not required - direct call
if (addEditPair.Dispatcher.CheckAccess())
method();
// Else if invoke is required - invoke
else
addEditPair.Dispatcher.BeginInvoke(dispatcherPriority, method);
,但我仍然得到同樣的錯誤。現在我很困惑!
任何想法的人?任何幫助,將不勝感激。
在這種情況下,您只能參考窗口。但是具有主動焦點的Windows邏輯將會丟失。 – Evgeny 2016-02-25 13:41:57