How to change WindowTaskScheduler RunDurationFor to Indefinitely

此生再无相见时 提交于 2019-12-11 12:27:59

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!