计划任务程序是 Windows Vista 以及即将发布的 Windows Server 2008 中一个很有用的附件。本文是一篇快速教程,讲解了如何在托管代码中使用计划任务程序。若要了解更多的详细解释,敬请访问 Bart De Smet 相应的博客文章。
Windows Vista 与即将发布的 Windows Server 2008 提供了一种可能,就是能够创建复杂的任务,使得它们可以在不同时刻及时运行。它还提供了一个很棒的特性,可以通过托管代码访问嵌入到操作系统中的计划任务程序的功能。首先,创建一个 C#控制台应用程序,然后从 System32 文件夹中导入 taskschd.dll 文件。它将会创建计划任务程序的 COM 互操作程序集。然后,像这样创建一个 TaskSchedulerClass 对象:
TaskSchedulerClass scheduler = new TaskSchedulerClass();
接下来连接 scheduler 对象:
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”;
当触发器中设置的条件满足要求时,任务就会运行。但是必须定义动作(action),否则任务什么都不会做。这是一个范例:
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,
“”);
任务一旦完成并被注册,就能够立即运行,如下所示,或者使用“schtasks /run”。
IRunningTask runTask = regTask.Run(null);
将这些代码集中在一起,则结果为:
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);
}
}
}
查看英文原文: Using the Task Scheduler in Vista and Windows Server 2008
评论