0

I have a wpf project, and I am trying to do a popup style window which is a separate wpf window I wrote. I am trying to do a simple Showdialog as I have done before on several other projects. I simply do the following from the MainWindow.xaml.cs

    PopupWindow myPopup = new PopupWindow();
    myPopup.ShowDialog()

At the PopupWindow() constructor I get an

The calling thread must be STA, because many UI components require this

Exception. I googled around, I think I understand the usual cause for the exception, I also saw the possible solutions.

  1. Add the STAThread property to the Main. Well its a WPF project, it should be generated as STAThread, and I also see in the generated Main that indeed it is.
  2. Launch the new window in a separate thread. I don't want to do that, I want this window to block the current Thread, and return the choice the user made on the window for the further run.

The original project was not created by me, I had to take it over from a colleague who left since. I can imagine that he did something nasty by creating the project and there are some setting wrong and that's why the main window is not the STAThread. I have however no idea what these setting could be and where I could start to look for them.

Have any of you seen this problem? Can you help me finding why the mainwindoe is not the STAThread?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Tamaska Janos
  • 61
  • 1
  • 6
  • Use the debugger, set a breakpoint on this statement. When it hits use Debug > Windows > Threads. With the expectation that you see this code running on a worker thread. Look at the Call Stack window to see how it got there, you need a Dispatcher.BeginInvoke() somewhere in that code. – Hans Passant Aug 15 '18 at 09:55
  • What's the contents of your MainWindow.xaml.cs? – mm8 Aug 15 '18 at 11:14
  • Thank you Hans Passant! I would never have thought to check it, but it is actually another thread where all the magic happens. Why, I do not understand really, but that is what happens when you work on somebody else's code. Now I understand the problem, I can start fixing it. Thank you! – Tamaska Janos Aug 15 '18 at 12:06

0 Answers0