8

From my mainform I call the following to open a new form

MyForm sth = new MyForm();
sth.show();

Everything works great however this form has a combobox that, when I switch its AutoCompleteMode to suggest and append, I got this exception while showing the form:

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 have set this attribute on my main function as requested by the exception:

[STAThread]
static void Main(string[] args)
{ ...

Can I please get some help as to understand what might be wrong.

Sample code:

private void mainFormButtonCLick (object sender, EventArgs e)
{
    // System.Threading.Thread.CurrentThread.SetApartmentState(ApartmentState.STA); ?
    MyForm form = new MyForm();
    form.show();
}

Designer:

this.myCombo.AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.Suggest;
this.myCombo.AutoCompleteSource = System.Windows.Forms.AutoCompleteSource.ListItems;
this.myCombo.FormattingEnabled = true;
this.myCombo.Location = new System.Drawing.Point(20, 12);
this.myCombo.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
this.myCombo.Name = "myCombo";
this.myCombo.Size = new System.Drawing.Size(430, 28);
this.myCombo.Sorted = true;
this.myCombo.TabIndex = 0; phrase";

Setting data source

public MyForm(List<string> elem)
{
    InitializeComponent();
    populateColorsComboBox();
    PopulateComboBox(elem);
}

public void PopulateComboBox(List<string> list )
{
    this.myCombo.DataSource = null;
    this.myCombo.DisplayMember = "text";
    this.myCombo.DataSource = list;
}
shytikov
  • 9,155
  • 8
  • 56
  • 103
santBart
  • 2,466
  • 9
  • 43
  • 66
  • What if you clean and rebuild the project? – Alvin Wong Nov 27 '12 at 08:59
  • Have you seen this similar [question](http://stackoverflow.com/questions/4685237/how-can-i-make-a-background-worker-thread-set-to-single-thread-apartment)? – Shiridish Nov 27 '12 at 09:01
  • How are you showing the form? Within the main thread or a separated thread? – Alvin Wong Nov 27 '12 at 09:04
  • I am not using any BackgroundWOrker, just create everything on the same thread as MainForm – santBart Nov 27 '12 at 09:07
  • BackgroundWorker runs in an seperate thread. Before showing the form try to set the AppartmentState: System.Threading.Thread.CurrentThread.SetAppartmentState(System.Threading.AppartmentState.STA); – Carsten Nov 27 '12 at 09:26
  • Can you post sample code what your trying to achive so that i can give a try at my end also? – Pratik Nov 27 '12 at 09:39
  • What are you using as AutoCompleteSource? – IvanL Nov 27 '12 at 10:19
  • Can you show the whole designer block that set-ups your combobox ? And also the code that sets up the autocomplete source ? – T. Fabre Nov 27 '12 at 10:21
  • Can you try to add `[STAThread]` before the `private void InitializeComponent()` in MyForm – t3hn00b Nov 29 '12 at 09:56
  • Such errors often occurs when main thread works under Multi Threaded Apartment(MDA). It's unclear to me - Is error still reproduced after you added [STAThread] attribute or you asking why do you need [STAThread] attribute in order to correct error? – nikita Nov 29 '12 at 10:16
  • It is working with [STAThread] but still error occurs – santBart Nov 30 '12 at 10:05
  • Are you sure you don't have any code that sets the current thread to MTA in your program ? – T. Fabre Nov 30 '12 at 11:06
  • 2
    If you add `if (Thread.CurrentThread.GetApartmentState() != ApartmentState.MTA) MessageBox.Show("Not STA");` before your call to `form.Show()`, does it show the message box ? – T. Fabre Nov 30 '12 at 12:22
  • hope you are showing your startup form by means of `Application.Run` method..and also what if you `showDialog` instead of `show`? – techBeginner Dec 06 '12 at 09:09

3 Answers3

3

Is Main(string[] args) really your entry point?

Maybe you have another Main() overload with no parameters. Or some other Main() in another class. Please open Project properties and look for the start object.

Jürgen Steinblock
  • 30,746
  • 24
  • 119
  • 189
2

Windows Forms applications must be run in the STA method.

See here: Could you explain STA and MTA?

And COM comes into play since windows forms comes into play since the controls themselves use native windows handles and thus must adhere to the STA model. I do believe that the reason you get the error at this specific place is that a second thread is created/ used internally by the AutoCompletion.

And as far as I have expereienced, the threading model must be set in Main, changing it later only works from STA to MTA, but not the other way round

Community
  • 1
  • 1
Mario The Spoon
  • 4,799
  • 1
  • 24
  • 36
1

As a wild thought: Create a deep copy of your source List in your second form and bind the combobox to the copy of the list rather than the original.

b0rg
  • 1,879
  • 12
  • 17