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.