But if we want to use task scheduler functionality in our .Net application then we
can use windows .dll so that we can customize the default user interface and
also we can provide control over it. For this implementation we can use “taskschd.dll“
file of Windows operating system.
There are some points when we go for task scheduling-
Task registration information
Here we need to provide basic details for task, like- Name of the task, Author, Task
description
Task settings
Here we have to set some setting based on that task will triggered, these are some
conditions to run the task if these conditions are satisfied then task will be
ready to execute.
Task trigger information
Here we need to set when the task will be executed; actually these are trigger time
information to trigger the task. Like – Start Time, End Time
Task action information
Here we need to set which action we want to perform. Like- we can give path of the
exe to execute on particular time.
Code Implementation-
For implementing task scheduler using .Net application first add the reference of
“taskschd.dll “ file. You can get it from –“C:\Windows\System32”. After adding the reference of
this .dll file you can use predefined class of this dll.
Now check the below code for complete implementation-
using System;
using System.Windows.Forms;
namespace TaskScheduler
{
public partial class frmTaskScheduler : Form
{
public frmTaskScheduler()
{
InitializeComponent();
}
TaskScheduler objScheduler;
//To hold Task Definition
ITaskDefinition objTaskDef;
//To hold Trigger Information
ITimeTrigger objTrigger;
//To hold Action Information
IExecAction objAction;
private void btnCreateTask_Click(object sender, EventArgs e)
{
try
{
objScheduler = new TaskScheduler();
objScheduler.Connect();
//Setting Task Definition
SetTaskDefinition();
//Setting Task Trigger Information
SetTriggerInfo();
//Setting Task Action Information
SetActionInfo();
//Getting the roort folder
ITaskFolder root = objScheduler.GetFolder("\\");
//Registering the task, if the task is already exist then it will be updated
IRegisteredTask regTask = root.RegisterTaskDefinition("SampleTask", objTaskDef, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
//To execute the task immediately calling Run()
IRunningTask runtask = regTask.Run(null);
MessageBox.Show("Task is created successfully");
}
catch (Exception ex )
{
MessageBox.Show(ex.Message);
}
}
//Setting Task Definition
private void SetTaskDefinition()
{
try
{
objTaskDef = objScheduler.NewTask(0);
//Registration Info for task
//Name of the task Author
objTaskDef.RegistrationInfo.Author = "AuthorName";
//Description of the task
objTaskDef.RegistrationInfo.Description = "SampleTask";
//Registration date of the task
objTaskDef.RegistrationInfo.Date = DateTime.Today.ToString("yyyy-MM-ddTHH:mm:ss"); //Date format
//Settings for task
//Thread Priority
objTaskDef.Settings.Priority = 7;
//Enabling the task
objTaskDef.Settings.Enabled =true ;
//To hide/show the task
objTaskDef.Settings.Hidden = false;
//Execution Time Lmit for task
objTaskDef.Settings.ExecutionTimeLimit = "PT10M"; //10 minutes
//Specifying no need of network connection
objTaskDef.Settings.RunOnlyIfNetworkAvailable = false ;
}
catch (Exception ex)
{
throw ex;
}
}
//Setting Task Trigger Information
private void SetTriggerInfo()
{
try
{
//Trigger information based on time - TASK_TRIGGER_TIME
objTrigger = (ITimeTrigger)objTaskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);
//Trigger ID
objTrigger.Id = "SampleTaskTrigger";
//Start Time
objTrigger.StartBoundary = "2014-01-09T10:10:00"; //yyyy-MM-ddTHH:mm:ss
//End Time
objTrigger.EndBoundary = "2016-01-01T07:30:00"; //yyyy-MM-ddTHH:mm:ss
}
catch (Exception ex)
{
throw ex;
}
}
//Setting Task Action Information
private void SetActionInfo()
{
try
{
//Action information based on exe- TASK_ACTION_EXEC
objAction = (IExecAction)objTaskDef.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
//Action ID
objAction.Id = "testAction1";
//Set the path of the exe file to execute, Here mspaint will be opened
objAction.Path = "mspaint";
}
catch (Exception ex)
{
throw ex;
}
}
}
}
In above code, I have written line by code to understand the logic. By reading the
comments you can easily get the purpose of the code. If the code is executed
without any error then after the execution of code you will get – “Task is created
successfully”.
You can check windows task scheduler to see created task, you will find an entry
like this-

I have configured “mspaint” for task so on trigger time it will open “mspaint”.
Here I have just created task using code but you want then you can design your own
UI to set all these values to create Task.
Code to delete Task-
If we want to delete existing task then we can delete it, Check the below code-
//Code to delete task
private void btnDeleteTask_Click(object sender, EventArgs e)
{
try
{
TaskScheduler objScheduler = new TaskScheduler();
objScheduler.Connect();
ITaskFolder containingFolder = objScheduler.GetFolder("\\");
//Deleting the task
containingFolder.DeleteTask("SampleTask", 0); //Give name of the Task
MessageBox.Show("Task deleted...");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Here "SampleTask" is the name of task. If given task is not exist then it will throw error.
You can download the complete code from here-
Download Source Code Example