views:

37

answers:

2

I'm about to write a control that has this defect. Why exactly is this bad?

Your ActiveX control has a common defect that single-threaded apartment (STA) ActiveX & COM objects must avoid: STA COM objects cannot perform blocking operations on the STA thread, unless the COM object also pumps Windows messages. Therefore, if your control needs to perform a synchronous blocking operation, it needs to implement a Windows message pump while waiting for the blocking operation to complete.

Source

I don't want to pump messages while I block because that seems to cause my javascript toexecute in reentrant fashion. which is really bad.

+3  A: 

You need to allow the STA thread to pump messages because otherwise, calls cannot be dispatched to the STA thread: Since there is already a thread in the apartment (and it's single threaded, duh) no other thread can enter the apartment, so posting a message to the threads (hidden) window is the "only" way to communicate with the STA thread.

This is used to process ORPC requests, but also to process messages not necessarily related to COM (i.e. GUI messages). Interrupting message processing by making a blocking call thus disrupts ORPC functionality and can potentially also block the user interface.

See Don Box "Essential COM" for thorough information on COM.

Jim Brissom
+1  A: 

You don't have pump anything manually. Try using CoWaitForMultipleHandles instead of MsgWaitForMultipleObjects in the snippet from the forum and let COM pump inter-thread call messages only.

wqw