August 13, 2001
URL:http://www.drdobbs.com/active-scripting-newsletter/184404681
by Mark M. Baker
Scripting Chat This month I received an e-mail from a developer regarding a problem he was having with IActiveScriptParse::ParseScriptText blocking and not returning when a script was parsed. I thought it would be useful to cover this method in some more detail since it is highly used in Active Scripting, but not necessarily well understood. First off, some background on the method for those newer to Active Scripting. When constructing an Active Scripting "host" (the code module that interacts with the scripting "engine" and manages the process of feeding script to the engine, handling events from the engine, etc.), one of the first steps after constructing the appropriate engine (VBScript, JScript, etc) is to feed script text into the engine itself. This is done by using IActiveScriptParse::ParseScriptText. The IActiveScriptParse interface is obtained from the engine via a normal COM IUnknown::QueryInterface call. The ParseScriptText method itself has a number of parameters that must be either used or defaulted depending upon the style of use. Let's take a look at these parameters in more detail:
It's important to understand that ParseScriptText blocks when evaluating an expression, and doesn't block when evaluating normal script text. The developer controls this by passing in the appropriate flags to the method. In the case of non-blocking script text, the script is actually executed when a call is made to IActiveScript::SetScriptState. The problem I think developers have with this method is its multiple use configuration. It is trying to serve two different roles, and it is often unclear which flags to pass in, when to pass them in, or which flags/values conflict with others. Ideally, Microsoft would have broken out the method into two or more methods that were narrow and easier to understand. But the interfaces are here to stay and it's important to understand the operation of this critical method in Active Scripting. Side Dish Microsoft's Andrew Clinick has been writing a superb series of articles regarding scripting in the .NET world. I highly recommend reading the Scripting Clinick column section on http://msdn.microsoft.com when you have time.
The Active Scripting FAQ
Here's a new entry in the upcoming FAQ release: Q: How can I safely interrupt a running script? A: To interrupt a running script, you call IActiveScript::InterruptScriptThread(). This is not guaranteed to immediately interrupt the script. Rather, it sets a flag that the script should stop, and the script engine will stop at the next opportunity. InterruptScriptThread is one of the only two methods you can call on a script engine from outside its base thread (the other being SetScriptSite). Since the thread where the script engine is running is obviously blocked while the script is in progress, you must call InterruptScriptThread from a different thread. This would also apply for reacting to the ESC key to call InterruptScriptThread. Since the thread isn't pumping messages while it's waiting for the script to complete, it won't detect that the ESC key has been pressed. This sample code assumes you've marshalled an IActiveScript interface pointer to the second thread and named it pScriptEngine.
Source: Joel Alley, Microsoft Developer Support Do You Have a Passion for Scripting? Check out the Dr. Dobb's Journal Alternative & Scripting Languages web site (http://www.ddj.com/topics/altlang/) for more information on a variety of scripting and alternative languages. This site changes regularly, so check back often. Be sure to bookmark Microsoft Program Manager Andrew Clinick's Scripting Clinick monthly column on the in's-and-out's of Active Scripting and the new Visual Studio for Applications (VSA) .NET technology. Looking for back issues of the Active Scripting newsletter? Head over to the Dr. Dobb's Journal site (http://www.ddj.com/maillists/active/) to read any of the back issues online. The Active Scripting newsletter has now been in publication for over a year and you will find a wealth of information, commentary and insight that will make using and understanding Active Scripting easier and more productive. Final Thoughts I hope you will find this newsletter a timely and helpful addition to your knowledge base about Active Scripting. Please feel free to email me at [email protected] and let me know what you think of it, ways to improve on content, format, etc., and how this newsletter is helping you. Until next month, Cheers! Mark |
Terms of Service | Privacy Statement | Copyright © 2024 UBM Tech, All rights reserved.