问题
I have the method below to create TaskScheduler for Windows using ASquare.WindowsTaskScheduler, and it works as expected.
public bool CreateTaskScheduler()
{
var file = $"{Setting.GetDirectory()}\\{Setting.GetSpacename()}.exe";
var response = WindowTaskScheduler
.Configure()
.CreateTask(TaskName, file)
.RunDaily()
.RunEveryXMinutes(5)
.Execute();
if (response?.ErrorMessage != null)
{
System.Console.WriteLine(response?.ErrorMessage);
}
return response != null && response.IsSuccess;
}
Now the requirement has change and I need to change the Duration time to Indefinitely.
So I tried to added following line to the code:
.RunDurationFor(TimeSpan.MaxValue)
It works but the max time was 168 minutes, I searched a bit and found this answer and this answer tried to change TimeSpan
inside .RunDurationFor
following:
try 1 new TimeSpan(0, 0, 0, 0, -1)
try 2 new TimeSpan(0, 0, 0, 0, Timeout.Infinite)
try 3 TimeSpan.FromMilliseconds(-1)
try 4 new TimeSpan(0, 23, 0, 0, 0)
When I run the code only try 4 create task Task Scheduler to 23 hours. But try 1, 2 and 3 does not return any exception but does not create the Task Scheduler.
My Question, any idea how what to write inside .RunDurationFor(//what to write here)
so it can be valid for creating an Indefinitely?
回答1:
After many hours of trail and fail. I assume ASquare.WindowsTaskScheduler has a bug or not updated. Off course if some one find a solution to ASquare I would still appropriate an answer, but till that comes, here is my alternative solution:
I have installed TaskScheduler 2.8 nuget package and it works as I wanted with the following code:
public void CreateTaskScheduler()
{
var file = $"{Setting.GetDirectory()}\\{Setting.GetSpacename()}.exe";
TaskService ts = new TaskService();
TaskDefinition td = ts.NewTask();
Trigger trigger = new DailyTrigger();
trigger.Repetition.Interval = TimeSpan.FromMinutes(5);
td.Triggers.Add(trigger);
td.Actions.Add(new ExecAction(file, null, null));
ts.RootFolder.RegisterTaskDefinition(TaskName, td);
}
So Indefinitely
duration is default by this code.
And here is a snapshot of the results I wanted:
Hope that helps.
来源:https://stackoverflow.com/questions/49258557/how-to-change-windowtaskscheduler-rundurationfor-to-indefinitely