Steps to Create and Delete Windows TaskScheduler instance in C#

Author - Navdeep Kaur
20.07.2017
|
0 Comments
|

Steps to Create and Delete Windows TaskScheduler instance in C# code:

1

Dependencies: Add reference of the taskschd.dll file. On click of start button following steps need to be executed to create an instance of Scheduler:

1. Create the instance of TaskScheduler:

TaskScheduler.TaskScheduler objScheduler = new TaskScheduler.TaskScheduler();

2. Call connect method: If instance of scheduler is created for remote machine then pass following parameters:

serverName
user
domain
password

objScheduler.Connect(ServerName,user,domain,password);

If instance of scheduler is created for local machine then there is no need to pass any parameter:

objScheduler.Connect();

3. Setting Task Definition: Create a Task Definition instance to set the following parameters:

Name of task
Description of task
Date of task Instance created
Execution time limit of task
Enable/disable the task

ITaskDefinition objTaskDef = objScheduler.NewTask(0);
//Name of the task AuthorName
objTaskDef.RegistrationInfo.Author = "AuthorName";
//Description of the task
objTaskDef.RegistrationInfo.Description = "TestScheduleruploader";
//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 = true;
//Execution Time Lmit for task
objTaskDef.Settings.ExecutionTimeLimit = "PT10M"; //10 minutes
//Specifying no need of network connection
objTaskDef.Settings.RunOnlyIfNetworkAvailable = false;

4. Setting Task Trigger Information: To execute task trigger after one hour, we need to create a TIME TRIGGER instance and set Repetition interval value set as “PT1H” similarly for two hour interval value set as “PT2H”.
In there StartBoundary property is used to set date/time when trigger starts to execute and EndBoundary property is used to set date/time when trigger stops executing:

if (chechHourly.Checked)
{
ITimeTrigger objTrigger = (ITimeTrigger)objTaskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);
//Trigger ID
objTrigger.Id = "TestScheduleruploader";
//Start Time
objTrigger.StartBoundary = datetostart.ToString("yyyy-MM-ddTHH:mm:ss");
//repeat hourly
objTrigger.Repetition.Interval = "PT1H";
//End Time
objTrigger.EndBoundary = "2018-01-01T07:30:00"; //yyyy-MM-ddTHH:mm:ss
}

To execute trigger daily create DAILY TRIGGER instance:

if (chkdaily.Checked)
{
IDailyTrigger objDailyDef = (IDailyTrigger)objTaskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY);
//Trigger ID
objDailyDef.Id = "TestScheduleruploader";
//Start Time
objDailyDef.StartBoundary = datetostart.ToString("yyyy-MM-ddTHH:mm:ss");
}

To execute trigger weekly create WEEKLY TRIGGER instance and set property DaysOfWeek as 1 for Sunday, 2 for Monday etc. WeeksInterval property specifies the interval after which trigger executes. By default it will execute after 7 days:

if (chkweek.Checked)
{
IWeeklyTrigger objWeekDef = (IWeeklyTrigger)objTaskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_WEEKLY);
objWeekDef.Id = "TestScheduleruploader";
objWeekDef.StartBoundary = datetostart.ToString("yyyy-MM-ddTHH:mm:ss");
objWeekDef.DaysOfWeek = 1;
objWeekDef.WeeksInterval = 1;
}

To Execute trigger monthly create MONTHLY TRIGGER instance:

if (chkmonth.Checked)
{
IMonthlyTrigger objMonDef = (IMonthlyTrigger)objTaskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_MONTHLY);
objMonDef.Id = "TestScheduleruploader";
objMonDef.StartBoundary = datetostart.ToString("yyyy-MM-ddTHH:mm:ss");
//execute trigger on last day of month
objMonDef.RunOnLastDayOfMonth = true;
}

5. Getting the root folder:

ITaskFolder root = objScheduler.GetFolder("\\");

6. Registering the task, if the task already exists then it will be updated:

IRegisteredTask regTask = root.RegisterTaskDefinition("TestScheduleruploader", objTaskDef, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");

7. To execute the task immediately call Run() method:

IRunningTask runtask = regTask.Run(null);

On click of stop button following steps are executed to delete the TaskScheduler instance.

1. Create TashScheduler instance:

TaskScheduler.TaskScheduler objScheduler = new TaskScheduler.TaskScheduler();

2. This method is used to connect the machine where you want to delete scheduler instance. If connecting to remote machine then use:

objScheduler.Connect(ServerName,user,domain,password);

If connecting to local machine then use:

objScheduler.Connect();

3. Get the root folder and specify a name of the instance in delete method which you want to delete:

ITaskFolder containingFolder = objScheduler.GetFolder("\\");
//Deleting the task
containingFolder.DeleteTask("TestScheduleruploader", 0);

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *