拼吾爱程序人生.Net编程Visual Studio.NET Using the Task Scheduler in Vista and Windows Server 2008

1  /  1  页   1 跳转 查看:743

Using the Task Scheduler in Vista and Windows Server 2008

Using the Task Scheduler in Vista and Windows Server 2008

Posted by Abel Avram  From/InfoQ

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);
        }
    }
}


 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。
 您可能对 [Visual Studio.NET] 的这些文章也感兴趣:

如何使用.Net来设计一个爬虫系统
NET4.0 CTP 中的WF
Mono 1.2发布及对Miguel de Icaza的采访
Windows Workflow Foundation 之旅---(4)工作流通信
如何获取当前路径?
面向Visual Studio 2008的XML Schema设计器
微软发布.NET 4.0 CTP、Visual Studio 2010 CTP和Azure工具CTP
Visual Studio的未来焦点:层级调用
ListView和CSS Friendly
New cool admin modules for IIS 7
 

Using the Task Scheduler in Vista and Windows Server 2008

在Vista和Windows Server 2008中使用计划任务程序

译/张逸

计划任务程序是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);
        }
    }
}
 
1  /  1  页   1 跳转

快速回复帖子

标题
禁用 URL 识别
禁用表情
禁用 Discuz!NT 代码
使用个人签名
  [完成后可按 Ctrl+Enter 无刷新发布]  

版权所有 拼吾爱程序人生    

Powered by Discuz!NT 2.1.202   Copyright © 2001-2008 Comsenz Inc. 鄂ICP备07500843号
返顶部