簡體   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