Taskscheduler类在XP中不起作用

时间:2013-03-27 22:28:09

标签: .net c#-4.0

嗨,我有一个安排任务的程序。虽然这在Windows7机器上运行良好,但它会在XP上引发错误。

以下是代码:

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());
            }
        }
    }
}

我在XP机器上运行此程序时遇到的错误是:

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)

有人可以告诉我在哪里出错。

谢谢, 莫妮卡

1 个答案:

答案 0 :(得分:3)

此代码使用的Task Scheduler 2.0接口仅从Windows Vista开始提供。此代码永远无法在Windows XP上运行。

后备选项将改为使用Task Scheduler 1.0接口,但由于可用功能存在很大差异,可能还不够。在这种情况下,您无能为力,因为操作系统根本无法提供您所需的服务。