C# .NET Windows Task Scheduler

The purpose of this article is to explore how to add scheduling task to windows scheduler using c#.net. Most of the time we need do scheduling job in .net or any other system.

The purpose of this article is to help to .net developer to add custom scheduling task to windows scheduler using c#.net.

Let's write a console application to perform a  schedule job.to this you need , open visual studio and select new Project menu , then select console application from list of template in window.look figure to more clear.




Lets write simple operation within the program.cs file.

Within the job performer application have followings lines of code to create a simple text file write date and time  into the file.

Code:
static void Main(string[] args)
{

using (FileStream stream=new FileStream(@"c:\schedulejobCreated.txt",FileMode.Append))
{
using (StreamWriter writer=new StreamWriter(stream))
{
writer.Write("Date and time" + DateTime.Now.ToLongDateString() + "-" + DateTime.Now.ToLongTimeString());
}
}
}

Next step we need create a GUI for configure schedule with Windows scheduler.that like be followings,

Here we need supply job name , that's mandatory field user name and password also you must supply to add task on the widows scheduler.

Lets say now have to schedule our SchedulerPeformer to a weekly job to create file in your local drive. to that you need write code like followings.

Note:here I'm suing a custom lib for access windows scheduler on the Machine.

when user supply all value on the GUI click Configure button to save.Lets say you are going configure like this GUI showing


The button click event code like be


private void btnConfigure_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtName.Text))
{
MessageBox.Show("Please enter task name");
return;
}
else if (cmbtaskTypes.SelectedIndex < 0)
{
MessageBox.Show("Please select the schedule task type");
return;
}
else if (string.IsNullOrEmpty(txttime.Text))
{
MessageBox.Show("Please enter time to begin task");
return;
}

short hour = short.Parse(txttime.Text.Substring(0, 2));
short minute = short.Parse(txttime.Text.Substring(3, 2));
using (ScheduledTasks tasks = new ScheduledTasks())
{
Task task = null;
if (tasks.OpenTask(txtName.Text.TrimEnd()) == null)
{
task = tasks.CreateTask(txtName.Text.TrimEnd());
}
// Fill in the program info
task.ApplicationName = "ScheduleJobPerformer.exe";// here you need give full path of the your exe
task.Comment = "Create file on the disk";

// Set the account under which the task should run.
task.SetAccountInformation(txtUserName.Text, txtpwd.Text.ToString());

// Declare that the system must have been idle for ten minutes before
// the task will start
task.IdleWaitMinutes = 10;

// Allow the task to run for no more than 2 hours, 30 minutes.
task.MaxRunTime = new TimeSpan(12, 0, 0);

// Set priority to only run when system is idle.
task.Priority = System.Diagnostics.ProcessPriorityClass.Idle;


if (cmbtaskTypes.SelectedText.Equals("Weekly"))
{
if (chkmon.Checked == true)
{
task.Triggers.Add(new WeeklyTrigger(hour, minute, DaysOfTheWeek.Monday));
}
if (chktus.Checked == true)
{
task.Triggers.Add(new WeeklyTrigger(hour, minute, DaysOfTheWeek.Tuesday));
}
if (chkwed.Checked == true)
{
task.Triggers.Add(new WeeklyTrigger(hour, minute, DaysOfTheWeek.Wednesday));
}
if (chktur.Checked == true)
{
task.Triggers.Add(new WeeklyTrigger(hour, minute, DaysOfTheWeek.Thursday));
}
if (chkfri.Checked == true)
{
task.Triggers.Add(new WeeklyTrigger(hour, minute, DaysOfTheWeek.Friday));
}
if (chksat.Checked == true)
{
task.Triggers.Add(new WeeklyTrigger(hour, minute, DaysOfTheWeek.Saturday));
}
if (chksun.Checked == true)
{
task.Triggers.Add(new WeeklyTrigger(hour, minute, DaysOfTheWeek.Sunday));
}
}
else if (cmbtaskTypes.SelectedText.Equals("Daily"))
{
task.Triggers.Add(new DailyTrigger(hour, minute, 1));
}
else
{
int []days =new int[] {4,12,15,20};
task.Triggers.Add(new MonthlyTrigger(hour, minute,days));
}
// Save the changes that have been made.
task.Save();
// Close the task to release its COM resources.
task.Close();

}
}

Note:You need add using statement on the top of the code file

using TaskScheduler;

when you clicked and configured, just verify its was create on on the windows scheduler.just go to the control panel, the select schedule tasks.

The look there task name as FileCreator,then double click on that.its look like followings,





Look nice everything fine. i hope this helpful to who are create schedule job with windows scheduler.

Download source code


Thank you
RRaveen
By Ravenet Rasaiyah   Popularity  (18726 Views)
Biography - Ravenet Rasaiyah
My name is RRaveen, and my profession is software developing since 2006 (having more than 4 years experience) and holding MCTS,MCPD certifications and most of developments are based on MS technologies .Specifically ASP.NET ,Sharepoint and Widows Mobiles. I have worked on ERP systems also.