Introduction The purpose of this article is to help to .net developer to add custom scheduling task to windows scheduler using c#.net. Implementaion 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.
C# Code
{codecitation class="brush: csharp; gutter: true;" width="650px"} 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()); } } } {/codecitation}
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 {codecitation class="brush: csharp; gutter: true;" width="650px"}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();
} }
{/codecitation}
Note:You need add using statement on the top of the code file {codecitation class="brush: csharp; gutter: true;" width="650px"} using TaskScheduler;
{/codecitation}
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 | About the Author |
 | | RRaveen | - B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions The summary of my skills: C#, VB.Net#,ASP.net, VC++, Java, WPF,WCF, Oracle, SQL Server, MS Access, Windows NT administration Occupation: Tech lead Location: Singapore Company: TGS Solutions
|
|
|