IScriptControlPtr bad multithreaded behaviour?
-
I have a VC++ program in which two IScriptControlPtr smart pointers are created in thread "1". Thread "2" calls the Run() method against the first IScriptControlPtr, and thread "3" calls the Run() method against the second one. All three threads call CoInitializeEx(NULL,COINIT_MULTITHREADED) first. What I am finding is, thread 2 (or 3) blocks until thread 3 (or 2) has finished, even though they are running against separate IScriptControlPtr objects (and no other thread is trying to access these smart pointers). Has anyone seen this behaviour before / is this to be expected? Is there perhaps bad multithreaded handling in msscript.ocx? (I am using version 1.0.0.7615). Many thanks, Neil.
-
I have a VC++ program in which two IScriptControlPtr smart pointers are created in thread "1". Thread "2" calls the Run() method against the first IScriptControlPtr, and thread "3" calls the Run() method against the second one. All three threads call CoInitializeEx(NULL,COINIT_MULTITHREADED) first. What I am finding is, thread 2 (or 3) blocks until thread 3 (or 2) has finished, even though they are running against separate IScriptControlPtr objects (and no other thread is trying to access these smart pointers). Has anyone seen this behaviour before / is this to be expected? Is there perhaps bad multithreaded handling in msscript.ocx? (I am using version 1.0.0.7615). Many thanks, Neil.
The
msscript.ocx
control is registered with the Apartment threading model. This means it has affinity with the thread on which it is created - COM will force calls from a different thread onto the thread which created the object. If you want to use multiple script controls concurrently, you should create them on the threads which will call their methods. I'm not sure, but I think using COINIT_MULTITHREADED with an Apartment object will cause you to get a proxy to the object even though you're calling an object on the same thread. Stability. What an interesting concept. -- Chris Maunder -
The
msscript.ocx
control is registered with the Apartment threading model. This means it has affinity with the thread on which it is created - COM will force calls from a different thread onto the thread which created the object. If you want to use multiple script controls concurrently, you should create them on the threads which will call their methods. I'm not sure, but I think using COINIT_MULTITHREADED with an Apartment object will cause you to get a proxy to the object even though you're calling an object on the same thread. Stability. What an interesting concept. -- Chris MaunderThanks for your reply, Mike. I suspected this might be the case. When you say "The msscript.ocx control is registered with the Apartment threading model" ... is this documented somewhere on the web? I spent some time searching, but don't recall that particular chestnut :-( It might be nice to have some definitive documentation for once, rather then relying on Google and trial and error!!!!
-
Thanks for your reply, Mike. I suspected this might be the case. When you say "The msscript.ocx control is registered with the Apartment threading model" ... is this documented somewhere on the web? I spent some time searching, but don't recall that particular chestnut :-( It might be nice to have some definitive documentation for once, rather then relying on Google and trial and error!!!!
It's not documented on the web AFAIK, but ActiveX controls are almost always registered with the Apartment threading model (although you'll see some not marked at all, therefore running on the primary thread, and some marked Both). An ActiveX control (as opposed to a component) is intended for use on a window. As for how
msscript.ocx
is registered, I usedregedit
and searchedHKEY_CLASSES_ROOT\CLSID
for that filename. I never asked why you're not just using the script engines directly. This page[^] suggests which you should use when. Stability. What an interesting concept. -- Chris Maunder -
It's not documented on the web AFAIK, but ActiveX controls are almost always registered with the Apartment threading model (although you'll see some not marked at all, therefore running on the primary thread, and some marked Both). An ActiveX control (as opposed to a component) is intended for use on a window. As for how
msscript.ocx
is registered, I usedregedit
and searchedHKEY_CLASSES_ROOT\CLSID
for that filename. I never asked why you're not just using the script engines directly. This page[^] suggests which you should use when. Stability. What an interesting concept. -- Chris Maunder