在 Windows 任务计划程序中更改已计划任务的运行时间 [英] Changing run time of already scheduled tasks in windows task scheduler

查看:35
本文介绍了在 Windows 任务计划程序中更改已计划任务的运行时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在修改机器上已存在的任务时遇到问题.我正在尝试使用从 C# 生成的互操作接口(从 system32/taskschd.dll 生成的 Interop.TaskScheduler.dll)来执行此操作.

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).

首先,我不能使用其他库,例如 http://taskscheduler.codeplex.com/.已经过测试,它可以与前面提到的库一起使用.现在,当我尝试对生成的接口做同样的事情时,没有任何变化.基本上我在做什么:

To start with, I can't use other libraries like 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"));

它不起作用.实际上,在那个 foreach 中它已经改变了,但是当我尝试调试并创建另一个打印 StartBoundary 值的 foreach 时,它显示了旧值.我做错了什么吗?有没有机会让它工作?:-) 谢谢.

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. Am I doing something incorrect? Is there any chance to get it working? :-) Thank you.

推荐答案

如果您尝试像这样使用 Task Scheduler C# API 更新任务,则需要声明一个新的 ITaskDefinition.如果您尝试更改现有定义然后重新注册,则不起作用.

If you are attempting to update a task using the Task Scheduler C# API like this, you need to declare a new 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.

这篇关于在 Windows 任务计划程序中更改已计划任务的运行时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆