There is a help file included with the download that provides an overview of the various classes. The Microsoft MSDN documentation provides an excellent overview of the Task Scheduler along with details around security and permission, idle conditions, and trigger repetition.
  • The Examples Page shows some C# code that demonstrates almost every function of the library.
  • The Installation Page explains how to use this library in your own projects and includes a sample project.
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 intermitant 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 tasks 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.

The Action class is an abstract class that is the foundation for four different actions. On 1.0, only the ExecAction specialization is available. 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.

Below is a brief example of how to use the library from C#.

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
   static void Main(string[] args)
   {
      // Get the service on the local machine
      using (TaskService ts = new TaskService())
      {
         // Create a new task definition and assign properties
         TaskDefinition td = ts.NewTask();
         td.RegistrationInfo.Description = "Does something";

         // Add a trigger that, starting tomorrow, will fire every other week on Monday
         // and Saturday and repeat every 10 minutes for the following 11 hours
         WeeklyTrigger wt = new WeeklyTrigger();
         wt.StartBoundary = DateTime.Today.AddDays(1);
         wt.DaysOfWeek = DaysOfTheWeek.Monday | DaysOfTheWeek.Saturday;
         wt.WeeksInterval = 2;
         wt.Repetition.Duration = TimeSpan.FromHours(11);
         wt.Repetition.Interval = TimeSpan.FromMinutes(10);
         td.Triggers.Add(wt)

         // Create an action that will launch Notepad whenever the trigger fires
         td.Actions.Add(new ExecAction("notepad.exe", "c:\\test.log", null));

         // Register the task in the root folder
         ts.RootFolder.RegisterTaskDefinition("Test", td);
      }
   }
}
Here's the same as above but in VB.NET

Imports Microsoft.Win32.TaskScheduler

Module Module1

    Sub Main()
        Using ts As New TaskService()
            ' Create a new task definition and assign properties
            Dim td As TaskDefinition = ts.NewTask
            td.RegistrationInfo.Description = "Does something"

            ' Add a trigger that will, starting tomorrow, fire every other week on Monday
            ' and Saturday and repeat every 10 minutes for the following 11 hours
            Dim wt As New WeeklyTrigger()
            wt.StartBoundary = DateTime.Today.AddDays(1)
            wt.DaysOfWeek = DaysOfTheWeek.Monday Or DaysOfTheWeek.Saturday
            wt.WeeksInterval = 2
            wt.Repetition.Duration = TimeSpan.FromHours(11)
            wt.Repetition.Interval = TimeSpan.FromMinutes(10)
            td.Triggers.Add(wt)

            ' Add an action (shorthand) that runs Notepad
            td.Actions.Add(New ExecAction("notepad.exe", "c:\test.log"))

            ' Register the task in the root folder
            ts.RootFolder.RegisterTaskDefinition("Test", td)
        End Using
    End Sub

End Module
If you're really into shorthand code, here's almost the same functionality as the C# code above, but much shorter:

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
   static void Main(string[] args)
   {
      new TaskService().AddTask("Test", new DailyTrigger { DaysInterval = 2 },
         new ExecAction("notepad.exe", "c:\\test.log", null));
   }
}

Last edited Nov 12, 2013 at 4:58 PM by dahall, version 17

Comments

sathiyajebac Jun 12 at 12:44 PM 
Hi,
I have some clarification and code sample for the following.
How to create a task with Administrator account.
How to updated the trigger settings for the existing task.
For example : I have a task which is running daily 12:30 AM, I would like to change the Date running and Time running for this task. I would like to update the date as current date and time as (current time + 5 mins).

How to invoke the task after update the trigger settings?

Thanks,

Jeba

Deepres Nov 21, 2013 at 11:55 AM 
works win7 is very good!

Deepres Nov 21, 2013 at 11:54 AM 
how to works windows xp?

dahall Aug 14, 2013 at 3:35 PM 
Dangerous: For the SecurityEditor compile problem, I have updated the source code so it will compile. Please note however, that this project is not actually used by the TaskScheduler assemblies. It was a side project that has not been completed.

Dangerous Aug 14, 2013 at 2:01 AM 
I can't get this to work. The download doesn't contain NativeMethods.cs, which is required for SecurityEditor and TestTaskService (and others, but I do have another version/instance of this class which is used elsewhere, albeit, without AccountLibs).

I also do not have the TaskScheduler.Trigger.CreateTriggers() method anywhere.

tmatute Nov 30, 2012 at 6:13 PM 
is there a way to load a XML with a task definition and register the task definition?

dahall Apr 27, 2012 at 4:16 PM 
Don't use the UserId property. Instead call the RegisterTaskDefinition method with "SYSTEM" as the user, null as the password, and TaskLogonType.ServiceAccount for the type.

RickBowden Apr 27, 2012 at 12:05 PM 
I've been trying this and it works great but I'm struggling with something.
I want to create scheduled tasks that run under the system account on both Windows 2008 and 2003.
If I create a task and use ...Principal.UserId = "SYSTEM"; it works for 2008 but not for 2003.
What am I doing wrong?