Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

JVM Languages

Extending JScript


Jan99: Threading Models and ActiveX Scripting

Dr. Dobb's Journal January 1999

Threading Models and ActiveX Scripting


JScriptTest is single threaded. Most nontrivial ActiveX Scripting hosts, however, will use more than one thread and will therefore be either apartment or free threaded.

Both the JScript and VBScript engines are marked as "Both" threaded. This means that they can be used in either apartment- or free-threaded hosts without incurring any marshaling overhead. To make writing hosts simpler, the ActiveX Scripting specification defines some rules about which threads may call the scripting engine at which time. Specifically, hosts should:

  • Choose a base thread (generally the thread that contains the message loop).
  • Instantiate the scripting engine in the base thread.
  • Call scripting engine methods only from the base thread, except where specifically allowed, as in the cases of IActiveScript::InterruptScriptThread and IActiveScript::Clone.
  • Call the scripting engine's dispatch object only from the base thread.
  • Ensure that the message loop runs in the base thread if a window handle is provided.
  • Ensure that objects in the host's object model are only source events in the base thread.

The documentation goes on to say "none of these restrictions apply to a host that chooses to implement a free-threaded IActiveScriptSite interface and a free-threaded object model. Such a host can use the IActiveScript interface from any thread, without restriction."

Unfortunately, this is not true for the current implementations of either JScript or VBScript. Calling any methods other than IActiveScript::InterruptScriptThread and IActiveScript::Clone from any thread other than the base thread will result in an error, even in a free-threaded host that implements a free-threaded IActiveScriptSite.

The reasoning behind this is efficiency. Scripts often interact with ActiveX controls, and these controls will often be apartment threaded. If the script engine could be called from any thread at any time, it would internally have to marshal all calls to such controls. If, however, it can guarantee that any individual script is called only on a single thread, no marshaling is necessary. This does, unfortunately, mean that hosts that want to call a single script from more than one thread will have to implement their own internal marshaling scheme to ensure adherence to the aforementioned rules.

-- P.B.


Copyright © 1999, Dr. Dobb's Journal

Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.