AspBucket offers ASP.NET, C#, VB, Jquery, CSS, Ajax, SQL tutorials. It is the best place for programmers to learn

Wednesday, 18 May 2016

How to schedule a task in windows scheduler using c#?

In this article I will discuss How to schedule a task in windows scheduler using c#? We can create & schedule jobs on windows scheduler using a c# code. You can download a scheduler dll from here.

Add scheduler "dll" reference in your application.


1- Create On time Job
    // Run only one time
    private void CreateTaskRunOnce()
    {       
       using (TaskService ts = new TaskService())
       {
         TaskDefinition td = ts.NewTask();
         td.RegistrationInfo.Description = "My on time task scheduler";
         td.Triggers.Add(new TimeTrigger(){StartBoundary=Convert.ToDateTime("19-05-2016 10:30:00")});
 
         td.Actions.Add(new ExecAction(@"C:/dummy.exe", null, null));
         ts.RootFolder.RegisterTaskDefinition("OnTime", td);
       }       
    }

2-Create daily task scheduler
    private void CreateTaskForDailyRun()
    {       
        using (TaskService ts = new TaskService())
        {
            TaskDefinition td = ts.NewTask();
            td.RegistrationInfo.Description = "Daily task scheduler";
 
            DailyTrigger daily = new DailyTrigger();
            daily.StartBoundary = Convert.ToDateTime(DateTime.Today.ToShortDateString()+" 10:10:00");
            daily.DaysInterval = 1;
            td.Triggers.Add(daily);
 
            td.Actions.Add(new ExecAction(@"C:/dummy.exe", null, null));
            ts.RootFolder.RegisterTaskDefinition("DailyScheduler", td);
        }
    }

3-Create Weekly Task
 private void CreateTaskForWeeklyRun()
    {       
        using (TaskService ts = new TaskService())
        {
            TaskDefinition td = ts.NewTask();
            td.RegistrationInfo.Description = "Weekly scheduler";
 
            WeeklyTrigger week = new WeeklyTrigger();
            week.StartBoundary = Convert.ToDateTime(DateTime.Today.ToShortDateString()+" 10:10:00");
            week.WeeksInterval = 1;
            week.DaysOfWeek =  Microsoft.Win32.TaskScheduler.DaysOfTheWeek.Monday |
                               Microsoft.Win32.TaskScheduler.DaysOfTheWeek.Tuesday;
            td.Triggers.Add(week);
 
            td.Actions.Add(new ExecAction(@"C:/dummy.exe", null, null));
            ts.RootFolder.RegisterTaskDefinition("MyWeeklyTask", td);
        }       
    }

4-Create Monthly Scheduler
private void CreateTaskForMonthlyRun()
    {       
        using (TaskService ts = new TaskService())
        {
            TaskDefinition td = ts.NewTask();
            td.RegistrationInfo.Description = Monthly scheduler";
 
             MonthlyTrigger mTrigger = new MonthlyTrigger();
             mTrigger.StartBoundary =Convert.ToDateTime(DateTime.Today.ToShortDateString()+" 10:10:00");
             mTrigger.DaysOfMonth = new int[] { 10, 20 };
             mTrigger.MonthsOfYear = MonthsOfTheYear.July | MonthsOfTheYear.November;
             // mTrigger.RunOnLastDayOfMonth = true; // V2 only
             td.Triggers.Add(mTrigger);
             td.Actions.Add(new ExecAction(@"C:/dummy.exe", null, null));
             ts.RootFolder.RegisterTaskDefinition("MyMonthlyTask", td);
        }       
    }

5- Create Tasks with parameter
Add below lines in your code
td.Actions.Add(new ExecAction(@"C:/dummy.exe", "paramerters", null));  //parmeter1 parmeter2 parmeter3
ts.RootFolder.RegisterTaskDefinition("SomeName", td);

6-Create Task that run multiple instances
Add below lines in your code

td.Settings.MultipleInstances = TaskInstancesPolicy.Parallel;

7-Delete Task Scheduler
private void DeleteTask()
    {
        using (TaskService ts = new TaskService())
        {
            if (ts.GetTask("TaskName") != null)
            {
                ts.RootFolder.DeleteTask("TaskName");
            }
        }
    }

8- Update Task Trigger
private void UpdateTaskTrigger()
{
        using (TaskService ts = new TaskService())
        {
            Microsoft.Win32.TaskScheduler.Task task = ts.GetTask("YourTaskName");
            TaskDefinition td = task.Definition;
            foreach (Microsoft.Win32.TaskScheduler.Trigger trigger in task.Definition.Triggers)
            {
                trigger.StartBoundary = DateTime.Now;
                trigger.Repetition.Interval = TimeSpan.FromHours(Convert.ToInt16(5));
            }
            ts.RootFolder.RegisterTaskDefinition("YourTaskName", td);
        }
}

9- Update Task and add new trigger
    // Add multiple instances
    private void UpdateTaskAddNewTrigger()
    {
        using (TaskService ts = new TaskService())
        {
            Microsoft.Win32.TaskScheduler.Task task = ts.GetTask("YourTaskName");
            TaskDefinition td = task.Definition;
 
            TimeTrigger trigger = new TimeTrigger();
            trigger.StartBoundary = DateTime.Now;
            trigger.Repetition.Interval = TimeSpan.FromHours(4);
 
            td.Triggers.Add(trigger);
            ts.RootFolder.RegisterTaskDefinition("YourTaskName", td);
        }
    }

   1 comment :

  1. This comment has been removed by a blog administrator.

    ReplyDelete

  • Popular Posts
  • Comments