Hi All
I have the following code, which runs a WPF window on it's own dedicated UI thread:
// 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();
This works great except when i try to set the Owner of this window, to a window that runs on the main Ui thread.
The exception i get is:
The calling thread cannot access this object because a different thread owns it.
I understand what the error means and why it happens, so then i implemented the following:
// If invoke is not required - direct call
if (addEditPair.Dispatcher.CheckAccess())
method();
// Else if invoke is required - invoke
else
addEditPair.Dispatcher.BeginInvoke(dispatcherPriority, method);
But i still get the same error. Now i'm confused!
Any ideas anyone? Any help would be appreciated.