Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

UI thread is blocking a background thread calling a COM object

I am working on an application that communicates with an external device via a third-party COM library. I'm attempting to have all communication to the device go through a background thread both to prevent problems with communication from screwing up my app and to get rid of some other complexities introduced by having communication in the UI thread.

The problem is that whenever something happens that causes the main UI thread to block (i.e. MessageBox.Show being called or even just moving the window around the screen), the communication with the device on the background thread stops as well.

Is there any way (short of a totally separate process) to break the two threads far enough apart that they won't interfere with each other? (Note, the exact same code with some math calculations to slow things down a bit works just fine, it's only when I'm using the COM library that I have the issue)

like image 304
AlphaKilo Avatar asked Dec 10 '22 16:12

AlphaKilo


1 Answers

The behavior you observe can be explained if the following two conditions are true.

  • The 3rd party COM library is designed to be run in a single threaded apartment
  • You are creating an instance of a class from the library on the UI thread

Since the UI thread runs in an STA (single threaded apartment) and the COM class was created on that thread then all calls to the class originating from a thread other than the UI thread will be marshalled onto the UI thread itself. If the UI thread is blocked then all calls to the COM class will block as well.

like image 115
Brian Gideon Avatar answered Feb 23 '23 01:02

Brian Gideon