1

我正在尝试创建一个计划任务(使用计划任务包装器)每 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");
            }
        }
    }
4

1 回答 1

0

我相信您正在寻找dailyTrigger.Repetition.Interval属性。

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 分钟重复一次

其他示例在此处

于 2013-04-22T03:03:12.557 回答