Task Scheduler Managed Class Library Overview

Introduction

The Task Scheduler Managed Class Library provides a single assembly wrapper for the 1.0 and 2.0 versions of Task Scheduler found in all Microsoft operating systems post Windows 98. It simplifies the coding, aggregates the multiple versions, provides an editor and allows for localization support.

Microsoft introduced version 2.0 (internally version 1.2) with a completely new object model with Windows Vista. The managed assembly closely resembles the new object model, but allows the 1.0 (internally version 1.1) COM objects to be manipulated. It will automatically choose the most recent version of the library found on the host system.

Project website: https://github.com/dahall/TaskScheduler

Class Overview

The TaskService class represents the machine specific instance of the system task scheduler. It provides access to information about the service, access folders (2.0 only), and can quickly add or retrieve a task. If you only need to use the library for very intermittent periods, wrap the TaskService instantiation in a using statement to easily disconnect at the end of your use. However, if you plan on using the connection to the Task Scheduler repeatedly, use an assembly level field to store the TaskService instance as connecting and disconnecting is an expensive operation.

Tasks are accessed and can be enumerated through a TaskFolder. For systems supporting only the 1.0 library, there is only the root folder. The 2.0 library supports a hierarchal structure similar to a file system. The Tasks property exposes a TaskCollection instance which can enumerate tasks and provides an indexer which allows access to individual tasks by name. The TaskCollection class also has methods that allow for the creation/registration and deletion of tasks and subfolders by name.

A task is represented by a Task instance. The Task class provides information about tasks state and history and exposes the task's TaskDefinition through the Definition property.

A TaskDefinition exposes all of the properties of a task which allow you to define how and what will run when the task is triggered. A task must have at least one action and one trigger defined.

Each task has a list of triggers that determine when the task will be run. These are accessed through the Triggers property of a task definition which exposes a TriggerCollection instance. TriggerCollection provides an indexer which allows access to individual triggers by their position in the list. The TriggerCollection class also has methods that allow for the addition and removal of triggers. TriggerCollection implements the IList interface so you can also enumerate all triggers using the foreach construct.

The Trigger class is an abstract class that forms the foundation of the different types of triggers that can be specified for a task. There are 10 different specializations that provide different ways to specify the time a task will run. Not all specializations work with the 1.0 library. See the help file for details about each of the trigger classes. The Trigger Documentation has some examples of how to setup each kind of trigger.

Each task also has a list of actions that determine what the task will do when triggered. These are accessed through the Actions property of a task definition which exposes a ActionCollection instance. ActionCollection provides an indexer which allows access to individual actions by their position in the list. The ActionCollection class also has methods that allow for the addition and removal of actions. ActionCollection implements the IList interface so you can also enumerate all actions using the foreach construct.

The Action class is an abstract class that is the foundation for four different actions. On 1.0, only the ExecAction specialization is available unless the PowerShellConversion option is enabled (see Action Documentation for more information). These actions determine what the service will do when a trigger is fired. The Action Documentation has some examples of how to setup each kind of action.

Namespaces

Microsoft.Win32.TaskSchedulerThe Microsoft.Win32.TaskScheduler namespace provides a wrapper for the 1.0 and 2.0 versions of Task Scheduler found in all Microsoft operating systems post Windows 98. It will automatically choose the most recent version of the library found on the host system. The namespace includes classes for direct manipulation of the Task Scheduler as well as UI items to allow users to manipulate tasks.
Microsoft.Win32.TaskScheduler.EventsIncludes classes to encapsulate event queries used for the EventTrigger.
Microsoft.Win32.TaskScheduler.FluentThe Fluent namespace includes classes used to expose the Fluent commands for creating tasks. These classes are not intended for use and cannot be instantiated. See the TaskService.Execute method for more information on creating tasks with the Fluent syntax.
Microsoft.Win32.TaskScheduler.UIComponentsThe Microsoft.Win32.TaskScheduler.UIComponents namespace includes Windows Forms controls that can be used to represent triggers and actions.