0

i have a class which, when a certain part of the gui is clicked will

-create a backgroundworker and do the following taks

-create a new object(this is a new windows form)

-call a method on it which gets some data

-populate the new windows form gui with that data

The problem is there is a gui component on the form which cant be created from outside of the main programme thread, if i do try i get the error

Active x .... cannot be instantiated because the current thread is not in a single-threaded apartment.

is there help people can offer so i can structure this?

in my do work

Community
  • 1
  • 1
ricki
  • 149
  • 1
  • 3
  • 10
  • Mb this one will work. http://stackoverflow.com/questions/1418466/single-threaded-apartment-cannot-instantiate-activex-control Anyway, in winforms you can implement a delegate and use Form.Invoke method to launch it in the main thread. – default locale Mar 31 '11 at 09:39
  • Is you application written in WinForms or WPF? – Indy9000 Mar 31 '11 at 09:41

2 Answers2

2

Don't create GUI components in a background thread. Use the background thread to get and process data, then render them in the UI in the main thread. I know that this in inconvenient, because

  • creating lots of UI elements can also take a lot of time and
  • creating them in the UI thread requires you to split your code into UI part and data processing part,

but there's not really a way around it. .NET UI components are not designed to be handled in background threads.

To perform only certain operations of your code in the main thread, you can use

  • someUIControl.Invoke(...) (WinForms) or
  • Dispatcher.Invoke(...) (WPF)

in the DoWork event handler of your BackgroundWorker. Alternatively, you can perform the UI operations in the RunWorkerCompleted event handler of your BackgroundWorker, which always executes in the UI thread.

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • would it then be stupid to have a class that deals with data retrieval and one thats then used to display that? and then i can run one in a background worker and one in the main thread when its finished? if this is possible should i be looking at any sort of inheritance heirarchy? – ricki Mar 31 '11 at 09:53
  • @ricki: Yes, it can be useful to split different tasks into different classes ("Single responsibility principle"). Whether it's necessary in your case or not is difficult to answer from a distance, because it always depends on the exact circumstances. I don't think any special kind of inheritance hierarchy is required in your case. If your code is simple, having two functions (one for the background thread and another for the UI thread) might suffice; if it's more involved, it might pay off to have two classes. – Heinzi Mar 31 '11 at 09:58
0

The UI should only be managed by the UI thread.

One possible solution would be to load the data asynchronously with the BackgroundWorker, and when it is done use the Result property of the DoWorkEventArgs to pass the results back to the UI thread and then display the new form.

Johann Blais
  • 9,389
  • 6
  • 45
  • 65