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.2k stars 191 forks source link

How do I add an in-code method to the scheduler? #904

Closed wmortume closed 2 years ago

wmortume commented 2 years ago

Task actions seems to only take in a string path or TAction but won't let me pass an in-code method. How would I do that?


public void Start()
{
    TaskDefinition task = TaskService.Instance.NewTask();
    task.RegistrationInfo.Description = GetName();
    DailyTrigger trigger = new DailyTrigger();
    trigger.StartBoundary = DateTime.SpecifyKind(DateTime.Today.AddHours(12).AddMinutes(45), DateTimeKind.Utc);
    task.Triggers.Add(trigger);
    task.Actions.Add(Elapsed()); //error
    TaskService.Instance.RootFolder.RegisterTaskDefinition("Scan", task);
}

public void Elapsed()
{
   //Read a file line
   //Send email of file line
}
dahall commented 2 years ago

This functionality is not available from the native Windows Task Scheduler. The task scheduler runs as an elevated service. Therefore, the inline execution you are suggesting would result in a significant security hole.

To run private code, you either need to call an executable with it, using ExecAction, or write a COM object per the ITaskServiceHandler interface (see home page), using ComHandlerAction.

wmortume commented 2 years ago

I'm confused on the com object, would I pass the method on the com handler? could you give a quick code example?

dahall commented 2 years ago

https://github.com/dahall/ITaskHandlerTemplate

You create a new project with the code in it. There is no way to pass a function.

BourgeoisDirk commented 1 year ago

What a bummer, seems to have alll the components i need, easy to use. but I cant go like "task.Actions.Add(() => ExecuteAction());" Well, that erases it from the realm of usable compoments :x