I am using a COM component which is absurdly slow (takes few minutes to give me requested data) so to keep the UI responsive, in my WPF application, I am thinking of using either Task or BackgroundWorker Classes to create a background thread, and disable all controls on UI to prevent any problems, as the component is not threadable.
The component is added to the project simply as a standard COM reference. The problem is that it simply doesn’t work (including initialization using new) from any background thread. It works fine from the UI thread. Any pointers will be appreciated.
Did you try to add
[STAThread]attribute toMain()?When the STAThreadAttribute is applied, it changes the apartment state of the current thread to be single threaded.
Without getting into a huge discussion about COM and threading, this attribute ensures the communication mechanism between the current thread and other threads that may want to talk to it via COM.
This attribute is usually automatically applied to Main when you start a new UI project.
Or (as SLaks told me) you can try:
You could also want to use
and check
resultvalue to be sure that your try was succesful