CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2011

    Using Windows Task Scheduler with C#

    I'm trying to create a schedule task with the Windows Task Scheduler in C#. It is failing when trying to register the task which is the following line of code:

    IRegisteredTask regTask = folder.RegisterTaskDefinition(

    The exception I get is as follows:

    System.Runtime.InteropServices.COMException: (4,36)ate:01/01/0001 12:00:00 AM

    I can't seem to find any good examples anywhere. The articles that I have come across lead me to believe that it might have something to do with the username and password when registering. Here's the code:

    public void Register(string path)

    //Conntect to scheduler
    TaskSchedulerClass scheduler = new TaskSchedulerClass();
    scheduler.Connect(null, null, null, null);

    //Create Task
    ITaskDefinition task = scheduler.NewTask(0);
    task.RegistrationInfo.Author = "Homeboy Software";
    task.RegistrationInfo.Description = name;
    task.RegistrationInfo.Date = created.ToString();
    task.Settings.Enabled = isEnabled;
    task.Settings.WakeToRun = true;

    //Set Task Trigger
    ITimeTrigger trigger = (ITimeTrigger)task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_TIME);
    trigger.Id = ID;
    trigger.Enabled = isEnabled;
    trigger.StartBoundary = "2011-01-10T22:30:00";
    //trigger.StartBoundary = startTime.ToString(); //Must be ISO 8601 date/time format specified on MSDN as: YYYY-MM-DDTHH:MM:SS(+-)HH:MM.

    //Set Task Action
    IComHandlerAction action = (IComHandlerAction)task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_COM_HANDLER);
    action.Id = ID;

    //Register Task
    //string taskPath = Path.Combine(path, Homeboy.Default.TasksPathFolder);
    ITaskFolder folder = scheduler.GetFolder("\\");
    IRegisteredTask regTask = folder.RegisterTaskDefinition(
    null, //User
    null, //Password
    "" //SDDL

    //Run Task
    IRunningTask runTask = regTask.Run(null);


  2. #2
    Join Date
    Nov 2010

    Re: Using Windows Task Scheduler with C#

    Use this + launching processes
    just call AT via a cmd process

  3. #3
    Join Date
    Dec 2002
    at home and at office :D

    Re: Using Windows Task Scheduler with C#

    I ve made good experiences using the


    then u can easily build tasks like

    TaskService ts = new TaskService();
    TaskDefinition td = ts.NewTask();
    td.Triggers.Add(new WeeklyTrigger(DaysOfTheWeek.Friday, 1));
    td.Actions.Add(new ExecAction("whatever.exe", null, null));
    ts.RootFolder.RegisterTaskDefinition("taskname", td);
    and this works on Windows7 as well
    Better ask and make a rhyme
    than search dead threads for a long time.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Windows Mobile Development Center

Click Here to Expand Forum to Full Width

On-Demand Webinars (sponsored)

We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.