0

I have a really strange issue in my C# Winforms application where if I load a specific form from inside another I get the error below

Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it

I do already have STA thread

[STAThread]
static void Main()
{
    System.Windows.Forms.Application.EnableVisualStyles();            
    System.Windows.Forms.Application.SetCompatibleTextRenderingDefault(false);
    System.Windows.Forms.Application.Run(new MainForm());
}

I am showing both forms as dialog.

I need this behaviour as I want them to be modal.

Does anyone have a workaround? I do have a call to an async method here but I need it.

Am I supposed to load my second form via a thread or something? If so, how?

private async void btnUsers_Click(object sender, System.EventArgs e)
{
  using (var secondForm = new MyForm(_service))
  {
    await secondForm .AsyncMethod().ConfigureAwait(false);
    secondForm.ShowDialog();
  }
}

This form loads fine straight from the application's main form.

Peter Duniho
  • 68,759
  • 7
  • 102
  • 136
Paul
  • 2,773
  • 7
  • 41
  • 96
  • I wish there was. This is happening when initialising a combo box – Paul Mar 22 '20 at 23:04
  • 3
    Don't do this `.ConfigureAwait(false);` you are telling the continuation to run on any threadpool thread apposed to the UI thread, if this is not the issue, there must be more to the problem. What is the the implementation of AsyncMethod() – TheGeneral Mar 22 '20 at 23:04
  • That doesn’t make any difference. I even tried removing the call – Paul Mar 22 '20 at 23:06
  • What @MichaelRandall said, plus I'm really confused why you are disposing of the form immediately after calling `Show` with the `using` block... – Zer0 Mar 22 '20 at 23:20
  • Sorry I meant ShowDialog I have updated the question – Paul Mar 22 '20 at 23:21
  • 1
    Ok, add the code for `AsyncMethod` plus get rid of `ConfigureAwait(false)`. If you can't post the code or it's too long, please post enough code so that we can compile and reproduce the issue. Also, that error should have a full stack trace giving you the line number the exception is thrown on. Did you not try debugging? – Zer0 Mar 22 '20 at 23:29
  • 1
    _"I do already have stat thread"_ -- no, obviously you don't. It is a major flaw in reasoning for a programmer to assume that when their programming environment emits an error, that the error is itself incorrect. That is practically never going to be the case, and being arrogant enough to think that one's code is fine and the error is wrong is going to make it so much harder for that programmer to find and fix their bug. – Peter Duniho Mar 23 '20 at 01:13
  • Considering I can see the code I think its pretty clear observation to make that does not imply any arrogance. StatThread is added by default. See amended question – Paul Mar 23 '20 at 01:17
  • 1
    Based on the code you posted, the problem is that you called `ConfigureAwait(false)`, causing `ShowDialog()` to be called in a thread pool thread, which won't be STA. To fix, just remove the `ConfigureAwait()`. Other possible problems include failing to set a thread as STA which it should be, or failing to use `Invoke()` or similar mechanism to get back to a thread that is already STA. See marked duplicates for all these variations. If those still don't help, post a new question, but this time make sure you provide a good [mcve] that reliably reproduces the problem. – Peter Duniho Mar 23 '20 at 01:18
  • _"StatThread is added by default"_ -- first of all, that's `STAThread`, not `StatThread`. `STA` as in "single-threaded apartment". Secondly, your program has more than one thread. The fact that the _main_ thread is STA doesn't in any way prove that whatever thread your code is executing in when you call `ShowDialog()` is STA. And in fact, the error message you get _is_ in fact proof of the opposite, no matter what you think. – Peter Duniho Mar 23 '20 at 02:16

0 Answers0