DNK Gif

Dot Net Knowledge

Labels

Tuesday, 7 July 2015

Scheduling Task/Application using C#

How to run a program on a scheduled  basis?

Well this can be done using TaskScheduler of Window.


Download the Sample Project here.

The code goes here

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32.TaskScheduler;

namespace Event.Schedule
{
    public class Scheduler
    {
        public static void ScheduleTask()
        {
            // Get the service on the local machine
            using (TaskService ts = new TaskService())
            {               
                // Create a new task definition and assign properties
                TaskDefinition td = ts.NewTask();
                td.RegistrationInfo.Description = "Umesh Does something";
                // Create a trigger that will fire the task at this time every other day
                DailyTrigger d = new DailyTrigger();
                d.DaysInterval = 1; // For daily Execution of Task
                d.StartBoundary = DateTime.Now.Date.AddHours(10);//Executes the Task daily at 10AM
                td.Triggers.Add(d);
                // Create an action that will launch another program or Task whenever the trigger fires
                td.Actions.Add(new ExecAction("C:\\Dev\\BirthDay\\BirthDay\\bin\\Debug\\MyApplication.exe"));
                // Register the task in the root folder
                ts.RootFolder.RegisterTaskDefinition("Umesh", td);
                // Remove the task we just created
                 ts.RootFolder.DeleteTask("Umesh");
            }

        }
    }
}

How to check the status of the Task/Task Details?

1. Go to Run, Type taskschd.msc + Enter

2. Now check the image below, you will get it.

No comments:

Post a Comment