-
Notifications
You must be signed in to change notification settings - Fork 196
Home
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.
- The Classes Overview describes each of the major classes and how they are used.
- The Examples Page shows some C# code that demonstrates almost every function of the library.
- Trigger Example Code has specific examples around triggers.
- Action Example Code has specific examples around actions.
- Task Event Management Code has examples around how to view and watch events related to tasks.
- Security Explanations and Samples show how to connect to remote servers and how to create tasks that run in different security contexts (system account, user account, elevated, etc.)
- PowerShell Examples has examples on how to use the library from PowerShell.
- The Installation & Samples Page explains how to use this library in your own projects and includes a sample project.
- System Requirements and Remote Troubleshooting provides a list of requirements for using and connecting to systems with the Task Scheduler library. 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)
{
// 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");