繁体   English   中英

如何通过c#安排自定义任务

[英]How to schedule custom task through c#

我正在使用任务计划程序在c#应用程序中安排我的任务。 我想我对这个库有了基本的了解。

但现在我陷入了一个我想要创建一个自定义动作的地方,该行动将按照设定的时间表执行。 就像 内置动作一样,EmailAction (将按照设定的时间表发送邮件), ShowMessageAction (将在集合上显示警告消息) schedule),我想创建一个将运行我的c#代码的动作,该代码将一些数据保存到我的数据库中。

我尝试的是:我创建了一个继承Action的类CustomAction ,如:

public class NewAction : Microsoft.Win32.TaskScheduler.Action
{
    public override string Id
    {
        get
        {
            return base.Id;
        }
        set
        {
            base.Id = value;
        }
    }

    public NewAction()
    {
    }
}

这是我的任务调度程序代码:

    ..  
    ... 
    // Get the service on the local machine
    using (TaskService ts = new TaskService())
    {
        // Create a new task definition and assign properties
        TaskDefinition td = ts.NewTask();
        td.RegistrationInfo.Description = "Does something";

        // Create a trigger that will fire the task at this time every other day
        TimeTrigger tt = new TimeTrigger();

        tt.StartBoundary = DateTime.Today + TimeSpan.FromHours(19) + TimeSpan.FromMinutes(1);
        tt.EndBoundary = DateTime.Today + TimeSpan.FromHours(19) + TimeSpan.FromMinutes(3);
        tt.Repetition.Interval = TimeSpan.FromMinutes(1);

        td.Triggers.Add(tt);

        // Create an action that will launch Notepad whenever the trigger fires
        td.Actions.Add(new NewAction());   <==========================

        // Register the task in the root folder
        ts.RootFolder.RegisterTaskDefinition(@"Test", td);

        // Remove the task we just created
        //ts.RootFolder.DeleteTask("Test");
    }
    ...
    ....

在线(箭头指向)我得到例外:

值不在预期范围内的任务调度程序中

我不确定我想要实现的目标是否可能,如果有可能请指导我正确的方向?

根据我对你的问题的理解。 我实现了相同的东西,但我使用了“Quartz”Scheduler而不是“Task Scheduler”。 它很容易实现。 也许你也可以试试这个。

参考: http//quartznet.sourceforge.net/tutorial/

如果我错了,请纠正我。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM