简体   繁体   English

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

[英]How to schedule custom task through c#

I am using Task Scheduler for scheduling my task in c# application. 我正在使用任务计划程序在c#应用程序中安排我的任务。 I think i got the basic understanding of this library. 我想我对这个库有了基本的了解。

But now i stuck in a place where i want to create a custom action which will execute on the set schedule.Like the built-in action ie EmailAction ( Which will send mail on set schedule ), ShowMessageAction ( Which will show alert message on set schedule ), i want to create an action which will run my c# code and that code will save some data to my database. 但现在我陷入了一个我想要创建一个自定义动作的地方,该行动将按照设定的时间表执行。 就像 内置动作一样,EmailAction (将按照设定的时间表发送邮件), ShowMessageAction (将在集合上显示警告消息) schedule),我想创建一个将运行我的c#代码的动作,该代码将一些数据保存到我的数据库中。

What I tried yet is: I created a class CustomAction which inherits Action , like : 我尝试的是:我创建了一个继承Action的类CustomAction ,如:

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

    public NewAction()
    {
    }
}

And here is my task scheduler code : 这是我的任务调度程序代码:

    ..  
    ... 
    // 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");
    }
    ...
    ....

On the line (pointed by arrow) i am getting the exception : 在线(箭头指向)我得到例外:

value does not fall within the expected range task scheduler 值不在预期范围内的任务调度程序中

I am not sure what i am trying to achieve is even possible or not, if it is possible than please guid me on the correct direction? 我不确定我想要实现的目标是否可能,如果有可能请指导我正确的方向?

According my understanding of your question. 根据我对你的问题的理解。 I had implemented same thing, but I had used "Quartz" Scheduler instead of "Task Scheduler". 我实现了相同的东西,但我使用了“Quartz”Scheduler而不是“Task Scheduler”。 It is very easy to implement. 它很容易实现。 May be you can also try with this. 也许你也可以试试这个。

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

Please correct me if I am wrong. 如果我错了,请纠正我。

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

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