dahall / TaskScheduler

Provides a .NET wrapper for the Windows Task Scheduler. It aggregates the multiple versions, provides an editor and allows for localization.
MIT License
1.21k stars 191 forks source link

Scheduled Task Creation failed #889

Closed puzzled-paradox closed 3 years ago

puzzled-paradox commented 3 years ago

Describe the bug When creating a Windows Scheduled Task, we received an error: " The current version of the native library (1.6) does not support the version of the "[Task Name]" task (0.0)"

To Reproduce Steps to reproduce the behavior: In the particular piece of code we are working with, there are two types of actions performed:

  1. TaskService.Instance.AllTasks in which it updates a scheduled task's Definition.Settings.ExecutionTimeLimit and Definition.Settings.StartWhenAvailable and then calls RegisterChanges()
  2. TaskService.Instance.AddTask in which it creates a new task with additional settings of the Definition.Principal.UserId, Definition.Settings.ExecutionTimeLimit, Definition.Settings.Enabled, and Definition.Settings.StartWhenAvailable and then calls Definition.Triggers.Clear() and RegisterChanges()

Environment (please complete the following information):

dahall commented 3 years ago

This is curious. I can only guess that there is a task on the machine that, when its XML is requested, returns one without a version tag. The code can be found here.

It could be the order or way that you're calling the methods on the retrieved tasks. Do you mind sending your code for the two examples above?

puzzled-paradox commented 3 years ago

Sure here is the code from the examples above.

TaskScheduler Examples.txt

dahall commented 3 years ago

I've run the following code without errors on my Windows 10 21H1 19043.1110 system with lib version 2.9.2.

foreach (Task t in TaskService.Instance.AllTasks)
{
   foreach (ExecAction action in t.Definition.Actions.OfType<ExecAction>().
      Where(a => string.Equals(a.Path, cmdPath, StringComparison.InvariantCultureIgnoreCase) &&
         string.Equals(a.Arguments, args, StringComparison.InvariantCultureIgnoreCase)))
   {
      t.Definition.Settings.ExecutionTimeLimit = TimeSpan.FromDays(1);
      t.Definition.Settings.StartWhenAvailable = true;
      t.RegisterChanges();
   }
}
var task = TaskService.Instance.AddTask("Temp", new DailyTrigger(1) { StartBoundary = DateTime.Now },
   new ExecAction(commandProgramPath, args), "SYSTEM", null, TaskLogonType.ServiceAccount);
task.Definition.Settings.ExecutionTimeLimit = TimeSpan.FromDays(1);
task.Definition.Settings.StartWhenAvailable = true;
task.Definition.Triggers.Clear();
task.Definition.Settings.Enabled = false;
task.RegisterChanges();
dahall commented 3 years ago

@puzzled-paradox Did the sample code above solve your issue?

puzzled-paradox commented 3 years ago

Yes, thank you very much