Chibueze Opata
Chibueze Opata

Reputation: 10054

Threading error in datagrid combobox control

I have a datagrid control in my project which has a combobox control manually filled with data, however the problem is that when I try to edit this value while adding a new row, I get a threading state exception:

DataGrid Error

The programs entry point method is set with the STAThread attribute, I have even placed the attribute on the method that calls the form with the control, still to no avail. Does anyone know how I can fix this problem? Thanks.

Upvotes: 0

Views: 324

Answers (1)

csharptest.net
csharptest.net

Reputation: 64228

The programs entry point method is set with the STAThread attribute...

This only applies to the main thread of the application. You must also be careful about threads you create that display this dialog box. For example:

    Thread t = new Thread(new ThreadStart(ThreadProc));
    t.SetApartmentState(ApartmentState.STA);
    t.Start();

If you did not directly create the thread you will need to change the code more drastically. For example if you are displaying the dialog box in Delegate.BeginInvoke() call, the thread will be the application's default thread pool. Since you do not have control over that thread's creation you can not create it as an STA thread. Instead you will need to create your own thread as above to display the dialog box.

Upvotes: 1

Related Questions