Working with Windows Scheduled Tasks
page 1 of 1
Published: 25 Apr 2006
Unedited - Community Contributed
Abstract
In this code snippet, Vishal demonstrates how to activate a C# console application using Windows Scheduled Tasks.
by Vishal Kumar Patil
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 36178/ 40

Overview

With Scheduled Tasks, you can schedule any script, program or document to run at a time that is most convenient for you.  Scheduled Tasks starts every time that you start Windows XP and runs in the background and it starts each task that you schedule at the time that you specify when you create the task (Reference: http://support.microsoft.com/default.aspx?scid=kb;en-us;308569&sd=tech).

The following sample application demonstrates the steps needed to work with the Windows Scheduled Tasks using a Console application.

Software Requirements

Microsoft Visual C# 2005 Express Edition

Create the Console application

Add the new Console Application project (with name as WindowsScheduledTasks) and paste the following code in it as shown below.

Listing 1

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
 
namespace WindowsScheduledTasks
{
  class Program
  {
    static void Main(string[]args)
    {
      StreamWriter sw = null;
 
      if(!File.Exists("d:/MessageLog.txt"))
      {
        sw =File.CreateText("d:/MessageLog.txt");
      }
      else
      {
        sw = File.AppendText("d:/MessageLog.txt");
      }
      string ReadString = "This message islogged at " + "DateTime : " +
        System.DateTime.Now;
      sw.WriteLine(ReadString);
      sw.WriteLine(
       "----------------------------");
      sw.Flush();
      sw.Close();
    }
  }
}

In the above listing, the program first checks whether MessageLog.txt exists or not.  If it does not exist, it is created.

Next, MessageLog.txt file is opened and a message is logged into the file using the StreamWriter object.  This is done so that when this console application is added to the Scheduled Tasks, it logs the message into the MessageLog.txt file at a specified time mentioned in the Scheduled Tasks.

Add the Console application to the scheduled Tasks.

To schedule a new task follow the steps as described below.

Click on Windows Start à Settings à Control Panel displays Scheduled Tasks folder in the Control panel as shown in Figure 1.

Figure 1

Double click the Scheduled Tasks folder in the above figure to open the Add Scheduled Task program as shown in below figure.

Figure 2

Click on Add Scheduled Task in Figure 2, this will run the Scheduled Task Wizard as shown below.

Figure 3

Clicking on the "Next" button will display a dialog as shown in Figure 4

Figure 4

 

Click on the “Browse” button to add the WindowsScheduledTasks.exe from D:\WindowsScheduledTasks\WindowsScheduledTasks\WindowsScheduledTasks\bin\Debug (or some other path where exe file resides) as shown in the above screen and then click Next.

Figure 5

 

To schedule the scheduler component Daily, check the “Daily” radio button as shown in the above screen and click next. These options should be chosen as per the User’s requirement.

Figure 6

Designate a Start time and Start Date; the Start Date will be the current date.

Figure 7

Enter any valid User & Password (with administrative rights) as shown in the above screen and click on "Next".

Figure 8

Select the Advanced properties option, and click on Finish. The following screen is displayed.

Figure 9

 

Click the Advanced button in the above screen, upon which the below screen is displayed where you can select the Repeat task.  With this setting, scheduler component (WindowsScheduledTasks.exe) will run every two minutes.

Figure 10

 

Verifying the Scheduler Execution

Verify a successful run of the scheduled task by navigating to D:\ drive and check the MessageLog.txt as shown in the below screen.

Figure 11

In this sample, we have seen that at a specified time Windows Scheduled Task’s WindowsScheduledTasks.exe component writes a message into the Messagelog.txt file.

Downloads

[Download Sample]

Conclusion

Working with Windows Scheduled Tasks is simple and straightforward.  In this article, we have also seen how we can use Windows Scheduled Tasks instead of windows Services.

With Scheduled Tasks, you can set up reminders or trigger e-mails at a specified time.  If you explore it, you can find many opportunities for its usage.



User Comments

Title: details   
Name: programer
Date: 2009-02-13 6:39:42 AM
Comment:
This is just a basic article.. nothing great in it. is this for students learning basics?
Title: API for Schedule Taks   
Name: Dnyaneshwar (Dyno)
Date: 2009-01-01 3:25:54 AM
Comment:
Check this API, This one is usefull for me.
http://www.mvps.org/emorcillo/en/code/shell/index.shtml
Title: passing parameters   
Name: Jackie
Date: 2008-08-29 6:25:42 PM
Comment:
yeah, how can you pass params to a Scheduled Task? (Couldn't see the comment response below)
Title: WOW   
Name: Sam
Date: 2008-08-21 8:27:58 AM
Comment:
GJDBY great job done by you
Title: Passing parameters   
Name: Rahul
Date: 2008-02-15 1:21:49 AM
Comment:
But if i wanted to pass some parameters to applicaiton through Schedular how can be done that??
Title: Wow   
Name: No name
Date: 2007-09-19 8:23:21 AM
Comment:
Now we know hot water
Title: Ndeke   
Name: Ndeke Bens
Date: 2007-08-24 4:47:14 AM
Comment:
Work well done. I have found a true solution to my problem. Though looks simple, its one of best solutions.
Title: Thank you   
Name: Tired of Lamers
Date: 2007-07-03 10:51:08 AM
Comment:
Thank you for the information, it was useful. Don't listen to Anonymous Cowards with their "Lame" responses.
Title: Working with Windows Scheduled Tasks   
Name: krunalsolanki@gmail.com
Date: 2007-06-07 7:41:14 AM
Comment:
hi thanks for your amazing work .
can u help me how we can pass parameters in scheduler
Title: how to create a scheduletask using API in windows application in VB.NET   
Name: jaya
Date: 2006-11-27 7:20:32 AM
Comment:
can u help me to cretae schedule task using API in windows application
Title: Lame   
Name: Anonymous Coward
Date: 2006-04-26 5:44:03 AM
Comment:
What is a manual for Scheduled Tasks doing on a development website? Just because you're running a small C# application doesn't make this a programming article. LAME.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2017 ASPAlliance.com  |  Page Processed at 2017-10-18 7:57:24 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search