Friday, 20 November 2015

Creating Windows Task Scheduler with Task Scheduler Managed Wrapper in C# & VB

While discussion with my mentor Serena Yeoh about creating Task Scheduler in code and we stumble upon Task Scheduler Managed Wrapper. This is a wrapper to the Windows Task Scheduler. For more detail, you can check it out from the following link. https://taskscheduler.codeplex.com/. There are other ways to create and manage the task scheduler, including PowerShell. But in this post, I'm going to just focus on Task Scheduler Managed Wrapper.

Creating task scheduler in code is pretty much straight forward. There are a few mandatory information you need to provide when creating a task scheduler, which is the name of the task scheduler, trigger (one time, daily, weekly, monthly) and the action (EG: execute an application). The rest of it are optional.

To use the Task Scheduler Managed Wrapper, you need to get the package from NuGet called "Task Scheduler Managed Wrapper". Once done, instantiate a new instance of TaskService.

[C#]
TaskService taskService = new TaskService();

[VB]
Dim taskService As TaskService = New TaskService()


TaskDefinition is required to be used for defining your actions and triggers, which is the mandatory information for creating task scheduler. TaskDefinition can also be used to define other optional information, such as task scheduler's description, when the task scheduler being created, who created the task scheduler and many more.

[C#]
TaskDefinition taskDefinition = taskService.NewTask();

[VB]
Dim taskDefinition As TaskDefinition = taskService.NewTask()


Once the TaskDefinition has been instantiated, we can start defining the trigger. In task scheduler, there are 4 different types of trigger (One Time, Daily, Weekly, Monthly). Each of them is using different trigger class (instantiate TimeTrigger() for [One Time], instantiate DailyTrigger() for [Daily], instantiate WeeklyTrigger() for [Weekly], instantiate MonthlyTrigger() for [Monthly]).

While all the triggers needs the start time by supplying the value to StartBoundary property, some of the trigger requires additional information. Such as the recurring days for daily schedule, recurring weeks for weekly schedule, the scheduled days of the week, and so on.

The following are the trigger example for daily. The task scheduler will be triggered every 2 days and the task scheduler will be triggered after 3 minutes from the current date time.

[C#]
DailyTrigger dailyTrigger = new DailyTrigger();
dailyTrigger.DaysInterval = 2;
dailyTrigger.StartBoundary = DateTime.Now.AddMinutes(3);

[VB]
Dim dailyTrigger As DailyTrigger = New DailyTrigger()
dailyTrigger.DaysInterval = 2
dailyTrigger.StartBoundary = DateTime.Now.AddMinutes(3)


Once you have define your trigger, add them to your TaskDefinition.

[C#]
taskDefinition.Triggers.Add(trigger);

[VB]
taskDefinition.Triggers.Add(trigger)


Next we will define the action for the task scheduler. ExecAction class will be used to define the application's executable path and its argument if any. The following code define the action to launch command prompt.

[C#]
ExecAction execAction = new ExecAction(@"C:\Windows\System32\cmd.exe");

[VB]
Dim execAction As ExecAction = New ExecAction("C:\Windows\System32\cmd.exe")


After define your action, add them to your TaskDefinition.

[C#]
taskDefinition.Actions.Add(execAction);

[VB]
taskDefinition.Actions.Add(execAction)


With both trigger and action defined, you can now create the task scheduler. The following code will create a task scheduler with a name "SampleTask" and it is located in the root path of your Task Scheduler Window.

[C#]
taskService.RootFolder.RegisterTaskDefinition("SampleTask", taskDefinition);

[VB]
taskService.RootFolder.RegisterTaskDefinition("SampleTask", taskDefinition)


You can check out the sample code here https://onedrive.live.com/redir?resid=E6612168B803803D!348&authkey=!AG20kICe6IVyxiU&ithint=file%2czip . For more examples, you can check it out from the official site https://taskscheduler.codeplex.com/wikipage?title=Examples.




No comments:

Post a Comment