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

查看:1236
本文介绍了改变已计划任务的运行时间在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);

有关它应该是工作先来看看,但由于某些原因,当我尝试分配为运行新的datetime在行:

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它改变了,但是当我试图调试,并创建了另一个的foreach它打印的价值StartBoundary它显示了旧值。我做得不正确的?是否有机会得到它的工作? :-)谢谢你。

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.

推荐答案

如果您尝试使用更新任务计划程序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天全站免登陆