Want to schedule a task that works on v1 or v2

Aug 10, 2009 at 9:40 PM

I'm trying to schedule a task that will work regardless of whether the target box is v1 or v2 Task Scheduler. I'm hoping I can use the same section of code for both, but I'm having trouble. Here's what I tried 1st, which worked on a v2 target box:

using (var action = new ExecAction(appName, appParams, null))
{
    using (var service = new TaskService(boxUncName))
    {
        var definition = service.NewTask();
        definition.Actions.Add(action);
        definition.Settings.AllowHardTerminate = false;
        definition.Settings.DeleteExpiredTaskAfter = TimeSpan.FromSeconds(1);
        definition.Principal.LogonType = TaskLogonType.InteractiveToken;
        using (var trigger = new TimeTrigger())
        {
            definition.Triggers.Add(trigger);
            trigger.StartBoundary = DateTime.Now.AddMinutes(2);
            trigger.EndBoundary = trigger.StartBoundary.AddHours(12);
            service.RootFolder.RegisterTaskDefinition(taskName, definition);
        }
    }
}

When I ran this against a v1 target box (Windows Server 2003), I got "System.Runtime.InteropServices.COMException (0x80041318): The task XML contains a value which is incorrectly formatted or out of range."
I tried logging the XmlText from the TaskDefinition at that point and got this:
<Task version="1.1" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo />
  <Triggers>
    <TimeTrigger>
      <StartBoundary>2009-08-10T13:46:33.031</StartBoundary>
      <EndBoundary>2009-08-11T01:46:33.031</EndBoundary>
      <Enabled>true</Enabled>
    </TimeTrigger>
  </Triggers>
  <Settings>
    <IdleSettings>
      <Duration>PT10M</Duration>
      <WaitTimeout>PT1H</WaitTimeout>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
    <DeleteExpiredTaskAfter>PT1S</DeleteExpiredTaskAfter>
    <Priority>7</Priority>
  </Settings>
  <Actions>
    <Exec>
      <Command>g:\ddfiles\dev10\tools\20810.00\razzle.cmd</Command>
      <Arguments>x86 chk exec Release /create:sources</Arguments>
    </Exec>
  </Actions>
</Task>
I also tried catching the exception and doing it a little differently in this situation:
using (var service = new TaskService(boxUncName, null, null, null, true))
{
    var definition = service.NewTask();
    definition.Actions.Add(action);
    definition.Principal.LogonType = TaskLogonType.InteractiveToken;
    definition.Settings.DeleteExpiredTaskAfter = TimeSpan.FromSeconds(1);
    using (var trigger = new TimeTrigger())
    {
        definition.Triggers.Add(trigger);
        trigger.StartBoundary = DateTime.Now.AddMinutes(2);
        trigger.EndBoundary = trigger.StartBoundary.AddHours(12);
        service.RootFolder.RegisterTaskDefinition(taskName, definition);
    }
}

That resulted in "System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.Win32.TaskScheduler.ExecAction.Bind(ITask v1Task)".
I'm running on a v2 host box. (Windows Server 2008.)
Any suggestions?
Thanks,
Rick
Aug 10, 2009 at 11:38 PM

Ok, I solved the NullReferenceException by creating a new ExecAction instead of re-using it. But I'd still like to know what is causing "The task XML contains a value which is incorrectly formatted or out of range" in hopes of creating the task correctly in the 1st place so that I don't have to catch the COMException.

Thanks,

Rick

Coordinator
Aug 13, 2009 at 2:22 AM

You should note that definition.Settings.AllowHardTerminate is a v2 only property, so you can't even set it for v1 machines. When I run your first code example without that line, it works. I believe that is what is giving you the "Out of range" exception. I'm not sure about the other errors. Are you running against the latest download (1.3.1)? Unfortunately, I don't have a remote v1 system to try this against so I'll have to ask you to do some testing.

Aug 13, 2009 at 6:13 PM

I removed the AllowHardTerminate and still have the same problem. (I'm using 1.3.1.) When I caught the COMException I logged the XmlText from the TaskDefinition:

<Task version="1.1" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo />
  <Triggers>
    <TimeTrigger>
      <StartBoundary>2009-08-13T11:04:35.583</StartBoundary>
      <EndBoundary>2009-08-13T23:04:00</EndBoundary>
      <Enabled>true</Enabled>
    </TimeTrigger>
  </Triggers>
  <Settings>
    <IdleSettings>
      <Duration>PT10M</Duration>
      <WaitTimeout>PT1H</WaitTimeout>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
    <DeleteExpiredTaskAfter>PT1S</DeleteExpiredTaskAfter>
    <Priority>7</Priority>
  </Settings>
  <Actions>
    <Exec>
      <Command>g:\ddfiles\dev10\tools\20813.00\razzle.cmd</Command>
      <Arguments>x86 chk exec Release /create:sources</Arguments>
    </Exec>
  </Actions>
</Task>