Using the Windows Task Scheduler for starting a program

I Love Xbase++ (ILX)
The portal for Xbase++ developers worldwide

Pat France

New member
Staff member
I am here to help you!
Aug 9, 2022
23
3
3
Customer Identifier
E114627
The Windows Task Scheduler can be used for running programs (tasks) automatically at certain events or at pre-defined intervals. This is also possible for programs written in Xbase++, of course. Thereby it does not matter whether the application is integrated as VIO (console) or PM (GUI) application.

Preconditions​

The following preconditions must be met for Task Scheduler to successfully start a task:
  • There must not be any dependency on the user's environment if the app is to be run without a logged in user. This means, for example, that the Xbase++ runtime must be deployed locally and can't be found using PATH. Similarly, mapped drives may not be available.
  • The user account the app is to be run under must have the "Log on as batch job" privilege (see Administrative Tools -> Local Security Policy -> Local Policies -> User Rights Assignment). Otherwise, starting your task may fail.
  • Applications executed in the background via the Task Scheduler should not have a user interface. Also refrain from using message boxes or other pop-up windows for user notifications. This is because windows popping up out of a sudden may be confusing to the user. Furthermore, a task waiting for user input may never complete. Instead, consider using other means such as the system's Event View.

Example​

We'll use the following simple program for setting up a task in Task Scheduler. The program simply opens a log file in the current directory and appends a message to the log.

Xbase++:
#include "fileio.ch"
#define CRLF Chr(13)+Chr(10)

PROCEDURE Main()
 LOCAL nF

   // Open the log file and position the file pointer at the end of the file
   nF := FOpen( "task.log", FO_WRITE )
   IF nF != -1
      FSeek( nF, 0, FS_END )
   ELSE
      nF := FCreate( "task.log" )
   ENDIF

   // .. Perform the actual task ..

   // Add a log entry for this task run
   FWrite( nF, "Task executed successfully on " + DtoC(Date()) + ", " + Time() + CRLF )

   FClose( nF )
RETURN

Step 1: Compile and test the program

Step 2: Run Task Scheduler by pressing WIN+Q and typing "task scheduler" into the search box, then pressing ENTER to run the application

Step 3: Create a task for running the app:
- Click on "Create Task" under "Actions"
1669297301688.png


- Enter the name of the new task, eg. "test" and make sure the correct user account is specified for running the app. Click "OK" to proceed.
1669297698290.png


- Switch to the "triggers" tab and click "New..."
1669297795557.png


- Select the correct trigger type, set the correct date and time for beginning the task. For this test, we'll want to run the task on a schedule and to run repeatedly every 5 minutes. When done, click "OK".
1669297975655.png


- Switch to the "Actions" tab and click "New..."
1669298170894.png


- Select the action to perform when running the task. In this case, we want the Task Scheduler to execute our app. We'll also specify a start directory to make sure the app can find its log file. Click "OK" to save changes.
1669298247163.png


The new task should now appear in the Task Scheduler Library and if all went well, should be executed automatically at the time and using the interval specified.

1669298586040.png

Using the Task History​

Sometimes a task may fail to start for some reason. Because tasks execute in the background, it's often difficult to find the issue preventing your task from executing. In such cases, the task history can often provide helpful clues as to what went wrong. The task history must be enabled globally for all tasks in Task Scheduler in the "Actions" panel.
1669298922774.png


Once the task history is enabled, Task Scheduler will log critical information for your task. Open your task and switch to the "History" tab to see it.

1669299062077.png

Miscellaneous​

  • Keep in mind that if the application is to be run without a logged in user, no interactive desktop may be available at the time the app is run.
  • Do not use quotes in the path entered into the "Start in (optional)" box. Otherwise, your task may fail to start!
  • Newly created tasks are not executed by default unless the system is on A/C power!

References​

 

Attachments

  • 1669298891742.png
    1669298891742.png
    32 KB · Views: 248
Last edited by a moderator: