Tuesday, January 10, 2006

[VBScript] Threads ... thin ones

The following is an attempt at threads in VBScript. The code owes a lot to Greg Chapman of MouseTrax Computing Solutions. There are two scripts, the first calling the second (in many instances). The means for communicating between the two is via the Volatile environment. When the script runs, Outlook flickers (don't know why).

There are comments, but the basic idea is to keep a watch over how many instances of cscript.exe there are running and while there's only so many, add more. Use the Volatile environment for semaphores and make sure there's enough delay in there to get stuff to and from Volatile (which is actually in Registry and not blazingly fast).

First the controller.
Then the controlled. There are quite a few extra useful routines in all that code, including an AADD(), and assorted WMI related routines.

I'm in the process of putting the thread functionality inside a Class so that I can change how things are done without causing drastic rewrites of the original VBScripts. I'm not entirely pleased with the Volatile environment and may end up playing with something else that can be used as a shared storage area between many programs ... like subdirectory structures.


Anonymous said...

How are these threads? you're spawning processes, no?

Bruce M. Axtens said...

Well ... (cough, cough) ... err ... umm ... yes, they're processes (blush). It's about the closest VBScript gets to threads. If I remember correctly, the MouseTrax article used the term and I blithely followed.

Andy said...

Once I finally understood what you're doing there, I caught myself quietly muttering...

"That's cool"

Neat concept.


Bruce M. Axtens said...

Thanks for dropping by, Andy. It's great to see that these articles are still being read after all these years.

Anonymous said...

You ranked 3rd on google for 'vbscript background thread' so I do appreciate your contribution; I am looking to incorperate this into a HTA so that I can get a more responsive UI, especially when I have a process that can run up to and beyond 8 hours [really].