如何将WindowTaskScheduler RunDurationFor无限期更改 [英] How to change WindowTaskScheduler RunDurationFor to Indefinitely
问题描述
我有以下方法使用
经过数小时的跟踪和失败.我假设ASquare.WindowsTaskScheduler有错误或未更新.当然,如果有人找到ASquare的解决方案,我仍然会给出一个答案,但是在此之前,这是我的替代解决方案:
我已经安装了
希望有帮助.
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?
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.
这篇关于如何将WindowTaskScheduler RunDurationFor无限期更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!