AspAlliance.com LogoASPAlliance: Articles, reviews, and samples for .NET Developers
URL:
http://aspalliance.com/articleViewer.aspx?aId=799&pId=-1
Working with Windows Scheduled Tasks
page
by Vishal Patil
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 9285/ 9

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.



©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-04-20 2:47:02 AM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search