Skip to content
David Hall edited this page Aug 21, 2019 · 4 revisions

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.

using System;
using Microsoft.Win32.TaskScheduler;

class Program
{
   static void Main(string[]() args)
   {
      // Create a new task definition for the local machine and assign properties
      TaskDefinition td = TaskService.Instance.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("notepad.exe", "c:\\test.log");

      // Register the task in the root folder of the local machine
      TaskService.Instance.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)
   {
      TaskService.Instance.AddTask("Test", new DailyTrigger { DaysInterval = 2 },
         new ExecAction("notepad.exe", "c:\\test.log", null));
   }
}

Alternately, you can use the library declaratively or "fluently":

Task t = TaskService.Instance.Execute("notepad.exe").WithArguments(@"c:\test.log").Every(2).Days().AsTask("Test");