Results 1 to 8 of 8

Thread: VB6 - Use Vista+ Task Scheduler 2.0 API

Threaded View

  1. #1

    Thread Starter
    PowerPoster dilettante's Avatar
    Join Date
    Feb 2006
    Posts
    24,487

    VB6 - Use Vista+ Task Scheduler 2.0 API

    One of the things that's always been a little clunky is using the Task Scheduler from code. Windows Vista made many changes to Task Scheduler, and also introduced a new 2.0 API with a COM interface quite usable from Visual Basic.

    You don't always need something like this but it's good to know it has gotten easier. One nice thing about it is that you can now defer long-winded processes to run in batch mode with lowered priority. You can even have Task Scheduler wake the machine up from sleep or hibernation to run your scheduled task.

    Because Task Scheduler opens a few vulnerabilities due to its nature (as do 3rd party "chron" type tools, perhaps even worse) it was changed during the life of Windows 2003 Server so that only an Administrator (or Backup Operator) can schedule tasks anymore. Interestingly, elevation isn't required unless you want the scheduled task to run elevated. The scheduling User simply needs to be a member of one of those two Groups. This is yet another reason why you should normally work as a Standard User, even with UAC enabled.


    Some Issues

    Now an Admininstrator can schedule tasks for other Users of course, by providing the User/PW. In Vista or later Windows versions the password will be stored in encrypted form, making things a bit more secure.

    But if a User is to have a scheduled task run while not logged on, that user needs the Group Policy setting "Log on as batch job."

    This applies whether the target User is an admin or not.

    In the "Home" editions of Windows there isn't an easy way to set this GPO. One tool I find handy is the free polsedit, which comes in both 32-bit and 64-bit builds. This can be handy even for higher-level editions of Vista, because you may be missing the Group Policy Management Console:
    If you are running Windows Vista with no service packs installed, the GPMC is included with the operating system. Upgrading to Windows Vista with Service Pack 1 removes the GPMC. To reinstall the GPMC, install the Remote Server Administration Tools (RSAT) for Windows Vista with SP1.

    The Demo

    I am including a small demonstration of using the COM interface to Task Scheduler that is new with the 2.0 API in Vista or later.

    The demo consists of two programs:
    • CreateDB - A VB6 program written to run in fully unattended mode, our batch worker program.
    • SchedCDB - A VB6 program used to set run parameters for CreateDB and schedule the task to run at a future time. This is a more typical VB6 GUI program.

    CreateDB accepts a command line with two parameters: the number of records to populate a sample database table with, and a log file prefix to use.

    CreateDB creates an empty MDB and MDW, adds a table to the MDB, then adds the supplied number of random records to it. All logging (including failures) should be recorded in the folder logs as a file named <logprefix>.log, in standard VB6 logfile format.

    SchedCDB presents a window with fields for the number of records, the logfile prefix, whether to run as the current user, user and password if not, whether to wake the machine, and when to start. It will create the logs folder if not present.

    Once you click the Schedule Task button an "Are you sure?" informational prompt occurs and if approved the task is scheduled. Then you can close SchedCDB.


    Closing Comments

    It's all pretty easy really.

    The new API opens up a lot of capabilities and you can get pretty exotic without the old rat's nest of 1.0 API calls required. Task Scheduler itself offers a few new features we didn't have before Vista.

    Note that if scheduling CreateDB to run under another user, that user will need read/write access to the program (and logs) folder!

    The attachment contains a ReadMe that covers a lot of ground, and suggests other things you might explore beyond what this simple demo does.

    It's great for overnight crunching operations like media transcoding, large data file transformation, or database maintenance.
    Attached Images Attached Images   
    Attached Files Attached Files
    Last edited by dilettante; Dec 31st, 2010 at 03:11 PM. Reason: reposted attachment, added error checks to catch bad passwords, etc.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width