Documentation Home Page

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.
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 Sep 4 at 6:41 PM by dahall, version 21

Comments

sathiyajebac Jun 12 at 1: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 12:55 PM 
works win7 is very good!

Deepres Nov 21, 2013 at 12:54 PM 
how to works windows xp?

dahall Aug 14, 2013 at 4: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 3: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 7:13 PM 
is there a way to load a XML with a task definition and register the task definition?

dahall Apr 27, 2012 at 5: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 1: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?