tags:

views:

47

answers:

1

I have a variation of a script that comes with Windows Media Encoder. I launch it via WSH and set up pipes to it's STDIN, STDOUT, and STDERR.

The script starts encoding and eventually waits in a DO loop checking the encoding object's status so the script knows to exit if encoding stops. (internally the loop just prints a period, and sleeps for about 2 sec).

To cleanly exit, I'd like to signal the script to stop encoding, which will allow it to drop from the loop and clean up.

I don't (or necessarily want to) have a COM object implemented in our app which launched the script - otherwise, I'd just trigger an event. (I'll reconsider if that's easier than I think to do in C++)

I thought of just sending it a character via STDIN to exit, but the problem is that all of the methods for WScript.stdin block. Is there any non-blocking way to read from stdin or some way to check for characters?

Is it possible to do a thread in VBS? as far as I can tell, you can only lauch other processes.

A: 

VBS is single threaded and there are no non-blocking calls you can use in the WScript object.

The classic VBS kludge to asynchronously signal a loop is to use the file system object. Your calling process and the VBScript need to agree on a folder location that is be monitored (lets call it c:\temp for now).

During initialisation your script does:-

 Dim fso: Set fso = CreateObject("Scripting.FileSystemObject")

In the loop your code does:-

 If fso.FileExists("c:\temp\signal.dat") Then
     fso.DeleteFile "c:\temp\signal.dat" 
     '' // Tidy up and exit loop
 End If

You calling process simply creates a file called "c:\temp\signal.dat" in order to instruct the script.

Its ugly but it works.

AnthonyWJones
That seems to be a common phrase with VBScript "It's ulgy but it works" They should change the name to IUBIWscript.
Tester101
At this point, I'm monitoring a registry entry (only polled once a second or two). Just as ugly, but I'm already writing / deleting the value.
Cary Tetrick