简体   繁体   English

在Windows任务计划程序中更改已计划任务的运行时间

[英]Changing run time of already scheduled tasks in windows task scheduler

I have problem with modifying tasks which already exists on machine. 我在修改已经存在于机器上的任务时遇到问题。 I'm trying to do this with generated interop interfaces from C# (Interop.TaskScheduler.dll generated from system32/taskschd.dll). 我正在尝试使用C#生成的互操作接口(从system32 / taskschd.dll生成的Interop.TaskScheduler.dll)执行此操作。

To start with, I can't use other libraries like http://taskscheduler.codeplex.com/ . 首先, 我不能使用其他库,如http://taskscheduler.codeplex.com/ Already tested and it works with library mentioned before. 已经过测试,它适用于之前提到的库。 Now when I try do same with generated interfaces nothing changes. 现在,当我尝试对生成的接口执行相同操作时,没有任 Basically what I'm doing: 基本上我正在做的事情:

string STR_DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
string taskName = "taskName", 
       user = "user", 
       pass = "pass";

DateTime nextRun = new DateTime.Now.AddDays(7);

TaskSchedulerClass ts = new TaskSchedulerClass();
ts.Connect(null, null, null, null);
IRegisteredTask task = ts.GetFolder("\\").GetTask(String.Format("\\{0}",taskName));

foreach (ITrigger t in task.Definition.Triggers)
    t.StartBoundary = nextRun.ToString(STR_DateTimeFormat.Replace(" ", "T"));

ts.GetFolder("\\").RegisterTaskDefinition(task.Path, 
                            task.Definition, 
                            (int)_TASK_CREATION.TASK_UPDATE, 
                            user, 
                            pass, 
                            _TASK_LOGON_TYPE.TASK_LOGON_PASSWORD, 
                            null);

For first look it should be working, but for some reason when I try to assign new datetime for run in line: 首先看它应该工作,但出于某种原因,当我尝试为运行分配新的日期时间时:

t.StartBoundary = nextRun.ToString(STR_DateTimeFormat.Replace(" ", "T"));

It doesn't work. 它不起作用。 Actually in that foreach it's changed but when I tried to debug and created another foreach which prints value of StartBoundary it shows old value. 实际上在那个foreach它已经改变但是当我尝试调试并创建另一个打印StartBoundary值的foreach时它显示旧值。 Am I doing something incorrect? 我做错了吗? Is there any chance to get it working? 有没有机会让它运作? :-) Thank you. :-) 谢谢。

If you are attempting to update a task using the Task Scheduler C# API like this, you need to declare a new ITaskDefinition. 如果您尝试使用此类任务计划程序C#API更新任务,则需要声明新的ITaskDefinition。 If you attempt to change the existing definition and then re-Register, it does not work. 如果您尝试更改现有定义然后重新注册,则它不起作用。

IRegisteredTask oldTask = ...
ITaskDefinition task = oldTask.Definition;

//modifications to oldTask.Definition / task

//does **not** work
folder.RegisterTaskDefinition(oldTask.Name, oldTask.Definition, ...
//does work
folder.RegisterTaskDefinition(oldTask.Name, task, ...

Answer credit goes to original poster, see comment on question. 答案信用到原始海报,请参阅问题评论。 I wrote up this answer to clarify the issue, and draw attention to the fact that the question had been answered, as a similar issue troubled me for a few days. 我写了这个答案来澄清这个问题,并提请注意这个问题已被回答的事实,因为类似的问题困扰了我几天。

public string ModifyScheduledTaskSchedule(string TaskName, string Date, string Hour, string Minute)
        {

            string ReturnedTask = TaskName;


                TaskScheduler.TaskScheduler ts = new TaskScheduler.TaskScheduler();

                ts.Connect(PackagingServer, PkgServerUserName, "DOMAIN", PkgServerPassword);

                IRegisteredTask task = ts.GetFolder("\\").GetTask(TaskName);

                ITaskDefinition td = task.Definition;

                td.Triggers[1].StartBoundary = Date + "T" + Hour + ":" + Minute + ":" + "00";

                ts.GetFolder("\\").RegisterTaskDefinition(TaskName, td, (int)_TASK_CREATION.TASK_UPDATE, "DOMAIN\\" + PkgServerUserName, PkgServerPassword, _TASK_LOGON_TYPE.TASK_LOGON_NONE, "");


            return ReturnedTask;
        }

Clark Bohs Clark Bohs

Yes. 是。 You have to create a new task definition with the same title and folder to update the existing one. 您必须使用相同的标题和文件夹创建新的任务定义以更新现有的任务定义。 Try to register it and see the task in Task Scheduler. 尝试注册它并在任务计划程序中查看任务。 It should have updated the task with the new schedule. 它应该使用新计划更新任务。 But, the history should remain the same. 但是,历史应该保持不变。

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

相关问题 Sitecore计划任务与Windows任务计划程序进行数据迁移 - Sitecore Scheduled Tasks vs Windows Task Scheduler for data migration Task Scheduler 2.0将当前计划的任务填充到WPF GridView - Task Scheduler 2.0 populating current scheduled tasks to WPF GridView 在Windows Task Scheduler中运行C#脚本? - run c# script in windows task scheduler? 在.net应用程序中从Windows Task Scheduler捕获任务 - Catch tasks from windows task scheduler in a .net app 与Windows计划任务相比,使用Web.config计划任务有什么好处 - What's are the benefits of using Web.config scheduled tasks over for example a Windows scheduled task 如何运行预制的 Windows 任务计划程序任务? - How to run Pre-made Windows Task Scheduler Task? 我可以编写一个C#程序 - 当作为计划任务运行时 - 检测任务计划程序何时尝试停止它 - can I write a C# program that - when run as a scheduled task - detects when Task Scheduler tries to stop it 在Windows 7中成功计划和运行的任务无法在Windows XP中运行 - Task that successfully scheduled and ran in Windows 7 could not run in Windows XP 使用任务计划程序托管包装程序的计划任务 - Scheduled Task using Task Scheduler Managed Wrapper 通过C#更改计划任务的“运行方式:”字段 - Changing the 'Run As:' field for a scheduled task via C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM