请协助安排任务
本文关键字:任务 | 更新日期: 2023-09-27 18:10:19
我试图创建一个计划任务(使用计划任务包装器)每3小时触发一次。我每天都要安装这个任务,但即使看了文档,也不能每隔几个小时运行一次。也不太确定LogonType。谁能告诉我我哪里做错了?
private void button1_Click(object sender, EventArgs e)
{
using (TaskService ts = new TaskService())
{
var androback = ts.GetTask("Andro_Inc_Backup");
bool taskExists = androback != null;
if (taskExists)
{
MessageBox.Show("Andromeda incremental backup task already installed");
}
else
{
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Incremental Backup";
td.Principal.LogonType = TaskLogonType.ServiceAccount;
td.Triggers.Add(new DailyTrigger { DaysInterval = 1 });
td.Actions.Add(new ExecAction("C:''Rameses''Program''Inc_Cloud_Backup.exe", null));
const string taskname = "Inc_Backup";
ts.RootFolder.RegisterTaskDefinition(taskname, td);
MessageBox.Show("Incremental Backup Task Installed");
}
}
}
我相信您正在寻找dailyTrigger.Repetition。时间间隔。
DateTime datetime = DateTime.Today;
DateTime startDateTime = new DateTime(datetime.Year, datetime.Month, datetime.Day, 0, 0, 0);
DailyTrigger dailyTrigger = new DailyTrigger { StartBoundary = startDateTime, Enabled = true, DaysInterval = 1 };
dailyTrigger.Repetition.Interval = TimeSpan.FromHours(23);
dailyTrigger.Repetition.Interval = TimeSpan.FromHours(3);
td.Triggers.Add(dailyTrigger);
你可以使用codeplex上的每周触发器示例作为参考,它添加了一个触发器,从明天开始,将在周一和周六每隔一周触发一次,并在接下来的11小时内每10分钟重复一次