VBScript and event handling under WSH

Posted on 2009-04-24
Last Modified: 2012-05-06

I would like to write a VBScript program using WSH that waits for incoming calls and then processes them as they come in.  I found the ActiveX controls that can deal with the telephony side of the application, but since it's a completely event driven application, what does the main part of the VBScript do while waiting for a call to come in and trigger an event?  I imagine that if there was a way to ping/query for an incoming call, then I could write a while  loop that sleeps for one second and then pings, although that seems so wasteful and inefficient.  But I don't think I have that functionality anyway, which raises the question of how do I get VBScript to twiddle  its thumbs (while waiting for an event to be triggered) until a call  comes in?  and if I do discover a function that can tell me if a call is coming in,  is the while loop I described above the best way to handle that?


Eyal Yaari
Question by:eyalyaari
    LVL 8

    Expert Comment

    The main part of your script will just be an infinite loop that waits for a trigger before taking any other action.  For Example:

      'Add your Event Trigger here probably in an If statement or Select Case

    Author Comment


    I tried running a tight loop like that and it used up 25 to 30% of my CPU. Is there a way to put it in sleep commands or something to that effect which won't eat up so much computer resources?  what would  happen if I put in a sleep command in the body of the loop, would it miss the events that happened during that time or would the events get queued up?  is there a kind of "sit idle and listen" method?

    Secondly, I don't have a property or method that can access the event trigger.  for example, while I am given the event, X_OnIncomingCall, I am not given any property or method that can tell me if there is any incoming call.

    Also, if I had a long "if" statement inside the loop that was checking for the event trigger and then triggering the corresponding subroutine, then aren't I copying the functionality of events in the first place?  I wouldn't need to use the built-in event handler since I would be doing the exact same thing manually, right?  Or have I missed the point...


    LVL 10

    Expert Comment

    you could use task manager to kick off the job at a particular time..infinite loop will likely cause the CPU to over process...if you know when the trigger will launch then the task manger can scehedule to run the script right after a trigger launches...

    Author Comment

    unfortunately, since the application is processing incoming phone calls it needs to be running all the time :-(

    Maybe this can't be done in VBScript?  Would I have the same problem if it was done in regular Visual Basic?

    Eyal Yaari
    LVL 76

    Accepted Solution

    Hi, Eyal.

    VBScript isnt typically a good platform for this kind of application.  VB or another high-level language is what youd want.  VBA and VB are substantially similar, so it might be possible to do this in Outlook.  Not sure thats a good choice though.  It might have a significant impact on Outlooks performance.  Its the difference between an executable and a script.  Scripts are by their very nature designed to run a series of commands and then end.  Programs are event driven and run until explicitly terminated.  If a program doesnt have anything to do, then it listens for events.  When an event comes in, then it executes code based on the event that occurred and resumes listening for events (unless the last event told the program to end).  As an example, think about Microsoft Word.  You launch Word and it appears on screen.  Until you start typing or make a menu selection Word is listening for events that pertain to it.  When you start typing, each key stroke generates an event telling Word to read the keyboard fires.  Word fetches the keystroke from the buffer and displays it in the document.  Make a menu selection and you generate another event causing Word to display the menu you clicked on.  Moving the mouse around inside the program generates mouse movement events which in turn tells Word to move the pointer, check to see where it is, display a different pointer depending on where the pointer is in the document, etc.  You dont have to write a loop for that to occur.  The event listener is implicit in every Windows program.  Scripts dont have an event listener.  There may be ways to emulate that in a scripting language, but I doubt that it can be done well.  Scripting languages and the interpreters that run them arent built with that capability in mind.  VBA has the ability to listen for events too.  Thats done by declaring a variable as WithEvents and then writing an event procedure for the event(s) you want to trap.  I dont think that listening for the events would be too much of a burden on Outlook, Im concerned about how Outlooks performance will be affected by the code necessary to process those events.  Naturally if you write this in Outlook, then Outlook will have to be running for the code to be able to listen for an incoming call or dial a number.  If Outlook is normally running all the time, then thats probably not an issue.

    Author Closing Comment

    excellent, that makes sense. I'll try it in Outlook VBA. Thanks.

    Eyal Yaari

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    This script will sweep a range of IP addresses (class c only, and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
    Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now