Search code examples
c#taskconsole-applicationtaskservice

Run Method in TaskService C# ?


I want to execute a function in given date just once and i'm done with the Task. What should i do?

static void Main(string[] args)
{
    DateTime date = GetExecuteTime();        
    using (TaskService ts = new TaskService())
    {
        // Create a new task definition and assign properties
        TaskDefinition td = ts.NewTask();

        td.RegistrationInfo.Description = "run method";

        TimeTrigger d = new TimeTrigger();
        d.StartBoundary = date;

        td.Triggers.Add(d);

        //td.Actions.Add();
    }

    Console.ReadKey(true);
}

Solution

  • Create console app, place it somewhere on your machine ,and create event in Task Scheduler and add a trigger when u want to run it.

    U can set trigger form another application , you can find answer here:

    Creating Scheduled Tasks

    Notice , you can't run some random function from your application without running the whole app from Task Scheduler.