0

The Code Behind of a WinForm is crashing with The calling thread must be STA, because many UI components require this., when trying to create a WPF user control, using myWpfUserControl = new MyWpfUserControl();. It seems to be crashing in the base user control constructor.

The entry point to my application is ApplicationStartup in App.xaml.cs, that has [STAThread] on it. I am not using threading at all, and I can see in the Threads pane in Visual Studio when it crashes that it is on the Main Thread.

EDIT: It used to work.

Here is the skeleton of my code:

[STAThread]
private void ApplicationStartup(object sender, StartupEventArgs e)
{
    LogonFormEventListener listener = new LogonFormEventListener();
    Logon.LogonFormEvent += new Logon.ClassFormEventHandler(listener.TestEventRaised);
    Logon logon = new Logon();
    logon.ShowDialog();
}

public partial class Logon : Form
{
    private void OKbutton_Click(object sender, EventArgs e)
    {
        if(LogonFormEvent !=null) LogonFormEvent(this, null);
    }
}

public class LogonFormEventListener
{
    public void TestEventRaised(Logon o, EventArgs e)
    {
        MainForm mainForm = new MainForm();
        mainForm.ShowDialog();
    }
}

public partial class MainForm : Form
{
    MyWpfUserControl myWpfUserControl;

    private void mainMenu_ItemClick(object sender, MenuBar.ItemEventArgs e)
    {
        switch (e.Item.Key)
        {
            case "myAction":
                myWpfUserControl = new MyWpfUserControl(); //CRASH
                ...
                break;
        }
    }
}

The user control crashes in the base constructor, before any of the code in my user control runs, so there is no point showing the user control code (I think).

Any ideas would be appreciated.

wezten
  • 2,126
  • 3
  • 25
  • 48
  • you need to add some code to diagnose. – Nikita Shrivastava Oct 08 '15 at 10:44
  • Possibly related to [this](http://stackoverflow.com/questions/1361033/what-does-stathread-do) question. – Codor Oct 08 '15 at 10:46
  • @Nikita What code would be helpful? – wezten Oct 08 '15 at 10:46
  • add your classes/usercontrols and main method. – Nikita Shrivastava Oct 08 '15 at 10:47
  • 1
    Wait, you're creating a WPF UserControl in the code behind of a "WinForm" and the startup is in app.xaml.cs (WPF)? I'm very confused as to what tech you're using and what you're trying to accomplish. – bic Oct 08 '15 at 10:48
  • @Nikita It's literally thousands of lines. – wezten Oct 08 '15 at 10:48
  • Show the calling code and describe the project makeup. – bic Oct 08 '15 at 10:49
  • Oh..no no..that much is fairly not needed. Can you just try with the skeleton sample of the app & not the logic, just the components.It might help. – Nikita Shrivastava Oct 08 '15 at 10:50
  • @bic Simple - ApplicationStartup calls `new MyWinForm().ShowDialog()`, and the Code Behind of MyWinForm it creates MyWpfUserControl. – wezten Oct 08 '15 at 10:50
  • The reason you are getting this issue is because an event is fired from a non UI thread. – Nikita Shrivastava Oct 08 '15 at 10:52
  • So your actual problem is trying to open a WinForms window in a WPF app. You need to be hosting the different UI tech in the corresponding ElementHost or use the InterOp helpers. http://stackoverflow.com/questions/16577122/open-winform-from-wpf-application – bic Oct 08 '15 at 10:54
  • @bic - no - the WinForm shows fine. See the code I posted. – wezten Oct 08 '15 at 11:05
  • Is the myWpfUserControl hosted in an ElementHost? https://msdn.microsoft.com/en-us/library/vstudio/ms754008(v=vs.90).aspx – bic Oct 08 '15 at 11:07
  • @bic - Yes, but it's not relevant - it crashes in the constructor, beforehand. – wezten Oct 08 '15 at 11:08
  • Could you try opening your WinForm using the WindowInteropHelper in the sample I linked to before. – bic Oct 08 '15 at 11:12
  • @bic Which form - Logon or MainForm or both? – wezten Oct 08 '15 at 11:14
  • @Nikita So why is VS crashing on the main thread? (btw I added code) – wezten Oct 08 '15 at 11:15
  • The [STAThread] attribute does nothing, it can only work on the Main() entrypoint of a program. And your ApplicationStartup event handler is most definitely not the entrypoint. WPF obfuscates it by auto-generating Main(), you'd have to take a look at obj\Debug\App.g.cs to see what might have gone wrong with it. – Hans Passant Oct 08 '15 at 11:35
  • @HansPassant App.g.cs has the STAThread attribute. – wezten Oct 08 '15 at 13:54

1 Answers1

1

Just a guess but it could probably the ShowDialog() on your TestEventRaised?

        System.Windows.Application.Current.Dispatcher.Invoke((Action)(() => { mainForm.ShowDialog(); }));

It might be firing for some reason.

If not, you could try to wrap it around your ShowDialog() calls on other constructors outside of the [STAThread] function, if ever some of them do that.

Tyress
  • 3,573
  • 2
  • 22
  • 45
  • Thanks, but your former suggestion didn't work. I didn't understand how to do the latter. – wezten Oct 08 '15 at 11:20
  • What I mean with the last part is, does your app have any other ShowDialog() calls outside of the ones you quoted in your question? Particularly in the class instantiation where it crashes. – Tyress Oct 08 '15 at 12:29