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
    Posts
    1

    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(
    "Sample",
    task,
    (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE,
    null, //User
    null, //Password
    _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN,
    "" //SDDL
    );

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

    }

  2. #2
    Join Date
    Nov 2010
    Posts
    34

    Re: Using Windows Task Scheduler with C#

    Use this + launching processes
    http://support.microsoft.com/kb/313565
    just call AT via a cmd process

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

    Re: Using Windows Task Scheduler with C#

    I ve made good experiences using the

    Microsoft.Win32.TaskScheduler.dll

    then u can easily build tasks like

    Code:
    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
  •  





Click Here to Expand Forum to Full Width

Featured