Reputation: 1211
I'm getting an "Invalid window handle
" exception when creating a Window
as per below code. This is invoked on a seperate thread. It is not thrown every time but occurs randomly. Also I cannot view the stack trace for the exception and it says "{Unable to evaluate expression because the code is optimized or a native frame is on top of the call stack.}
"
private void ShowDialog()
{
Thread thread = new Thread(() =>
{
waitContainer = MakeSimpleWindow();
this.waitContainer.Closed += new EventHandler(waitingWindow_Closed);
waitContainer.ShowDialog();
System.Windows.Threading.Dispatcher.Run();
});
thread.SetApartmentState(ApartmentState.STA);
thread.IsBackground = true;
thread.Start();
}
public Window MakeSimpleWindow()
{
Window w = new Window(); // Exception occurs from here
w.Title = Attributes[MessageBoxAttribute.message];
return w;
}
Upvotes: 2
Views: 4451
Reputation: 273244
The 2 lines:
waitContainer.ShowDialog();\
System.Windows.Threading.Dispatcher.Run();
at the very least have a race condition. When ShowDialog starts executing, the Thread is not yet running a Dispatcher.
But the answer is not how to fix this. While you can execute more that 1 dispatcher, it is rarely a good idea. Look for a solution where you run 1 GUI thread (the main thread) and resolve the other issues with Invoke and events.
Upvotes: 5