Apply this attribute to the entry point method (the Main()
method in C# and Visual Basic). It has no effect on other methods.
Your calling code is in an MTA thread. Applying the
[STAThread]
attribute to the method it calls will have no effect.
You need to test the current thread's apartment state, and spin up a new STA thread if necessary.
public static void LoadForm()
{
if (Thread.CurrentThread.ApartmentState == ApartmentState.STA)
{
LoadFormCore();
}
else
{
ThreadStart run = LoadFormCore;
Thread t = new Thread(run);
t.SetApartmentState(ApartmentState.STA);
t.Start();
t.Join();
}
}
private static void LoadFormCore()
{
ObjectForm form1 = new ObjectForm();
form1.ShowDialog();
}