InfoQ

News

Using the Task Scheduler in Vista and Windows Server 2008

Posted by Abel Avram on Feb 28, 2008 04:56 AM

Community
.NET
Topics
Programming,
.NET Framework
Tags
C#,
Windows Vista

Task Scheduler is an useful addition to Windows Vista and the upcoming Windows Server 2008. This is a quick lesson on how to use the Task Scheduler from managed code. For a more detailed explanation, please visit the corresponding Bart De Smet's blog page.

Windows Vista and the upcoming Windows Server 2008 offer the possibility to create complex tasks which can be run at various moments in time. It would be great to have access to this functionality embedded into the OS from managed code. The first step is to create a C# Console Application, then import the taskschd.dll library found in the System32 folder. This will create the TaskScheduler COM interop assembly. Then create a TaskSchedulerClass like this:

TaskSchedulerClass scheduler = new TaskSchedulerClass();

 The next step is to connect to the scheduler:

TaskSchedulerClass scheduler = new TaskSchedulerClass();
scheduler.Connect(null, null, null, null);

The next step is to create a task, and choose one of the many settings it can have:

ITaskDefinition task = scheduler.NewTask(0);
task.RegistrationInfo.Author = "Author";
task.RegistrationInfo.Description = "New Task";
task.Settings.RunOnlyIfIdle = true;

Following is choosing a moment when the task should start. That is done by means of triggers. Our example will use a daily trigger as shown:

IDailyTrigger trigger = (IDailyTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY);
trigger.Id = "DailyTrigger";
trigger.StartBoundary = "2008-01-01T12:00:00";
trigger.EndBoundary = "2008-01-31T12:00:00";

The task will run when the conditions set in the trigger are met. But an action has to be defined, otherwise the task will do nothing. This is an example:

IEmailAction action = (IEmailAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_SEND_EMAIL);
action.Id = "Email action";
action.Server = "server...";
action.From = "sender...";
action.To = "recipient...";
action.Subject = "The subject of the email...";
action.Body = "The body text of the email...";

The task is almost ready to be used. It just needs to be registered.

ITaskFolder folder = scheduler.GetFolder("\\Task");
IRegisteredTask regTask = folder.RegisterTaskDefinition(
    "Test",
    task,
    (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE,
    null, //user
    null, // password
    _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN,
    "");

The task is completed and registered. It can be run now as shown below, or by using "schtasks /run".

IRunningTask runTask = regTask.Run(null);

Putting it all together results:

using System;
using System.Collections.Generic;
using System.Text;

namespace TaskScheduler {
    class Program {
        static void Main (string[] args) {
            TaskSchedulerClass scheduler = new TaskSchedulerClass();
            scheduler.Connect(null, null, null, null);

            ITaskDefinition task = scheduler.NewTask(0);
            task.RegistrationInfo.Author = "Author";
            task.RegistrationInfo.Description = "New Task";
            task.Settings.RunOnlyIfIdle = true;

            IDailyTrigger trigger = (IDailyTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY);
            trigger.Id = "DailyTrigger";
            trigger.StartBoundary = "2008-01-01T12:00:00";
            trigger.EndBoundary = "2008-01-31T12:00:00";

            IEmailAction action = (IEmailAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_SEND_EMAIL);
            action.Id = "Email action";
            action.Server = "server...";
            action.From = "sender...";
            action.To = "recipient...";
            action.Subject = "The subject of the email...";
            action.Body = "The body text of the email...";

            ITaskFolder folder = scheduler.GetFolder("\\Task");
            IRegisteredTask regTask = folder.RegisterTaskDefinition(
                "Test",
                task,
                (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE,
                null, //user
                null, // password
                _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN,
                "");

            IRunningTask runTask = regTask.Run(null);
        }
    }
}

No comments

Reply

Exclusive Content

Typemock: Past, Present and Future

Eli Lopian of Typemock answers a few questions on Typemock origins and where Typemock is headed.

Agile in Practice: What Is Actually Going On Out There?

Scott Ambler talks about actual data resulting from surveys made during 2006-2008, showing how Agile is perceived and implemented within organizations.

Building Smart Windows Applications

From QCon 2008, Daniel Moth presents on using Visual Studio 2008 and .NET 3.5 to create compelling rich Windows applications.

Joshua Kerievsky about Industrial XP

Joshua Kerievsky, founder of Industrial Logic, talks about Industrial Extreme Programming which extends XP by including practices dealing with management, customers and developers.

Jeff Barr Discusses Amazon Web Services

Amazon Web Services (AWS) Evangelist Jeff Barr discusses SimpleDB, S3, EC2, SQS, cloud computing, how different Amazon services interact, origins of AWS, AWS globalization and the March AWS outage.

More Than Just Spin (Up) : Virtualization for the Enterprise and SaaS

Cloud services have helped bring virtualization to the forefront. Its full power however, also includes other benefits such as high availability, disaster recovery, and rapid provisioning.

Ruby Beyond Rails

John Lam talks about his path to dynamic languages, some of the problems of making IronRuby run fast, and how the DLR helps with implementing languages.

VMware Infrastructure 3 Book Excerpt and Author Interview

VMware Infrastructure 3: Advanced Technical Design Guide and Advanced Operations Guide provides a wealth of practical insights into setting up virtualization in todays corporate environments.