gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 20:39:47 26 4
gpt4 key购买 nike

我正在使用 Task Scheduler用于在 c# 应用程序中安排我的任务。我想我对这个图书馆有了基本的了解。

但现在我卡在了一个地方,我想创建一个将按设定的时间表执行的自定义操作。就像 built-in actionEmailAction(将按设定的时间表发送邮件)、ShowMessageAction(将按设定的时间表显示警报消息),我想创建一个将运行我的 C# 代码的操作该代码会将一些数据保存到我的数据库中。

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

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

在线(箭头所指)我收到异常:

value does not fall within the expected range task scheduler

我不确定我正在努力实现的目标是否可能,如果可能的话请指导我正确的方向?

最佳答案

根据我对你问题的理解。我已经实现了同样的事情,但我使用了“Quartz”调度程序而不是“任务调度程序”。它很容易实现。也许您也可以试试这个。

引用: http://quartznet.sourceforge.net/tutorial/

如有错误请指正

关于c# - 如何通过c#安排自定义任务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15988583/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com