I am using an old COM component (communications class library) in a new C# application. The COM control handles communication with some of our instruments. One of the functions is to download开发者_运维问答 data, which, depending on the size of data, can be a rather lengthy operation. The new C# app is set up to handle the downloading as a thread. The problem is, when the code is executing the download function, everything else freezes up, so I cannot click a 'Cancel' button in the UI. I've added a Sleep () (trying everything from 0 to 1000 as params) call to the download code after every block without success. The part that has me confused is if my download thread, instead of calling into the COM control, simply executes an infinite loop in C#, I can cancel out of that because the UI is active. Does anyone have any thought on what I can try to make the download thread yield?
Thanks
If the threading model of the COM component is STA and you are creating it in the main UI thread and then calling the method to download data from a secondary thread the call will be marshaled and executed back on the UI thread and basically blocking your application.
STA COM components require to be created in a STA thread that pumps messages because all calls to the component will be marshaled and executed in the thread that created it independently of the thread that actually calls a method on the COM component.
精彩评论