Creating a New Trigger

To create a trigger you must use three interfaces. IScheduledWorkItem provides the IScheduledWorkItem::CreateTrigger method for creating the trigger object, ITaskTrigger provides the ITaskTrigger::SetTrigger method for setting the criteria for the trigger, and the COM interface IPersistFile provides a Save method for saving the new trigger to disk.

The following procedure describes how to create a new trigger.

To create a new trigger

  1. Call CoInitialize to initialize the COM library and CoCreateInstance to get a Task Scheduler object. (This example assumes that the Task Scheduler service is running.)
  2. Call ITaskScheduler::Activate to get the ITask interface of the task object. (Note that this example gets the "Test Task" task.)
  3. Call CreateTrigger to create a trigger object. (Note that CreateTrigger is inherited from IScheduledWorkItem.)
  4. Define a TASK_TRIGGER structure. Note that wBeginDay, wBeginMonth, and wBeginYear members of TASK_TRIGGER must be set to a valid day, month, and year respectively.
  5. Call ITaskTrigger::SetTrigger to set the trigger criteria.
  6. Save the task with the new trigger to disk using IPersistFile::Save. (The IPersistFile interface is a standard COM interface supported by the ITask interface.)
  7. Call Release to release all resources. (Note that Release is an IUnknown method inherited by ITask.)
For a code example of See
Creating a new trigger for an existing task C/C++ Code Example: Creating a Task Trigger

 

Task Scheduler 1.0 Examples