Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I have a program that schedules task. While this runs fine on Windows7 machine, it throws an error on XP.

Here is the code:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaskScheduler;

namespace MyTaskScheduler
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                string STR_DateTimeFormat = "yyyy-MM-dd HH:mm:ss";
                string taskName = "MyTask";
                string taskFolder = "MyTaskFolder";

                Console.WriteLine("Creating task");
                ITaskService taskScheduler = new TaskSchedulerClass();
                taskScheduler.Connect(null, null, null, null);

                ITaskFolder rootFolder = taskScheduler.GetFolder(@"\");
                bool _found = false;
                foreach (ITaskFolder fol in rootFolder.GetFolders(0))
                {
                    if (fol.Name == taskFolder)
                        _found = true;
                }
                if (!_found)
                    rootFolder.CreateFolder(taskFolder, null);
                ITaskFolder folder = rootFolder.GetFolder("\\" + taskFolder);

                _found = false;
                foreach (IRegisteredTask tsk in folder.GetTasks(0))
                {
                    if (tsk.Name == taskName)
                        _found = true;
                }  
                if (_found)
                    folder.DeleteTask(taskName, 0);

                Console.WriteLine("Creating new task");
                ITaskDefinition taskDef = taskScheduler.NewTask(0);
                taskDef.RegistrationInfo.Description = "Opens notepad at the scheduled times";
                taskDef.RegistrationInfo.Author = "Monica";
                taskDef.Settings.Enabled = true;
                taskDef.Settings.Hidden = false;
                taskDef.Settings.DisallowStartIfOnBatteries = false;
                taskDef.Settings.WakeToRun = true;

                IWeeklyTrigger trigger = (IWeeklyTrigger)taskDef.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_WEEKLY);
                //if (intWeekDays != 0)
                trigger.DaysOfWeek = (short)10;
                trigger.Id = "MyTimeSet";
                trigger.StartBoundary = DateTime.Now.Date.AddHours(10).AddMinutes(15).AddSeconds(20).ToString(STR_DateTimeFormat.Replace(" ", "T"));

                IExecAction action = (IExecAction)taskDef.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_EXEC);
                action.Id = "MyRun";

                string actionFile = "notepad.exe";

                action.Path = String.Concat("\"", actionFile, "\"");

                action.Arguments = String.Format("argus \"{0}\"", "argument1");

                IRegisteredTask regTask = null;

                regTask = folder.RegisterTaskDefinition(taskName, taskDef, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_NONE, "");

                Console.WriteLine("task created");                
            }
            catch (Exception e1)
            {
                Console.WriteLine(e1.ToString());
            }
        }
    }
}




The error I get when I run this program on XP machine is:

C#
System.Runtime.InteropServices.COMException (0x80040154): Retrieving the COM class     factory for component with CLSID {0F87369F-A4E5-4CFC-BD3E-73E6154572DD} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).   at TaskScheduler.Program.Main(String[] args)



Can someone advise where am I going wrong here.

Thanks, Monica
Posted
Updated 27-Mar-13 13:05pm
v2
Comments
[no name] 27-Mar-13 20:27pm    
What version of .NET is installed on the XP machine?
Prasad Khandekar 27-Mar-13 21:05pm    
Hello Monica,

I think you are trying to use COM wrapper for The task scheduler. It's available on Windows Vista & Above. See this KB (http://technet.microsoft.com/en-us/subscriptions/aa381833(v=vs.85).aspx)

1 solution

There are two versions of the Task Scheduler API. Your TaskScheduler class is apparently using version 2.0, which doesn't exist on XP, only on Vista and above.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900