سؤال

I am using a STA COM object from a back ground thread, one of the COM object's a method will block, when i call it from a new thread which is in STA threading mode since the COM object's mode is STA, and the UI thread seems gets blocked too, can i avoid this UI blocking?

هل كانت مفيدة؟

المحلول

That's the point of STA, COM will automatically marshal the call from your worker thread to the STA thread. So that the COM server methods are always called in a thread-safe way. And if the method blocks, that will also block your STA thread. Usually the UI thread.

This means for one that using this COM object in a thread doesn't actually accomplish anything. For another that the real problem is in the COM server, it shouldn't be blocking. Do make sure this isn't a deadlock situation.

A possible workaround is to create the COM server on another STA thread so at least your UI thread doesn't get blocked. Check this answer for sample code. Both the Thread.SetApartmentState and the Application.Run calls are crucial to create a hospitable home for the server.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top