Create Windows Task Scheduler using C#

No.of Views10589
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  15 Feb 2010 22:02:47
Tag : CSharp , How to
Create Windows Task Scheduler using C#
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

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.

Image Loading...


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,

Image loading...

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

Image loading...

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,





Image Loading...

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


 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- 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
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top