Throwing an exception when trying to create a new Task Schedueler
I am trying to create a new task in Windows Task Scheduler in C #. What I have so far is pretty much copy and paste http://bartdesmet.net/blogs/bart/archive/2008/02/23/calling-the-task-scheduler-in-windows-vista- and-windows-server-2008-from-managed-code.aspx
Everything compiles just fine, but it's runtime. I am getting the following exception:
Unable to pass COM object of type 'System .__ ComObject' to interface type 'TaskScheduler.ITimeTrigger'. This operation failed because the call to QueryInterface in the COM component for the interface with IID '{B45747E0-EBA7-4276-9F29-85C5BB300006}' failed due to the following error: such an interface is not supported (exception from HRESULT: 0x80004002 (E_NOINTERFACE )).
Here's all the code so you can see what I'm doing here without following the link above.
TaskSchedulerClass Scheduler = new TaskSchedulerClass();
Scheduler.Connect(null, null, null, null);
ITaskDefinition Task = Scheduler.NewTask(0);
Task.RegistrationInfo.Author = "Test Task";
Task.RegistrationInfo.Description = "Just testing this out.";
ITimeTrigger Trigger = (ITimeTrigger)Task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY);
Trigger.Id = "TestTrigger";
Trigger.StartBoundary = "2010-05-12T06:15:00";
IShowMessageAction Action = (IShowMessageAction)Task.Actions.Create(_TASK_ACTION_TYPE.TASK_ACTION_SHOW_MESSAGE);
Action.Id = "TestAction";
Action.Title = "Test Task";
Action.MessageBody = "This is a test.";
ITaskFolder Root = Scheduler.GetFolder("\\");
IRegisteredTask RegisteredTask = Root.RegisterTaskDefinition("Background Backup", Task, (int)_TASK_CREATION.TASK_CREATE_OR_UPDATE, null, null, _TASK_LOGON_TYPE.TASK_LOGON_INTERACTIVE_TOKEN, "");
The line that throws the exception is
ITimeTrigger Trigger = (ITimeTrigger)Task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY);
The exception message makes sense to me, but I'm afraid I don't know enough about COM to really know where to start.
Also, I must add that I am using VS 2010 and I needed to set the project to either x86 or x64 processor instead of the usual "Any CPU" because it kept giving me a BadImageFormatException. I doubt this is related to my current problem, but just in case I thought I could mention this as well.
a source to share
You are using a different enum value, for TASK_TRIGGER_DAILY you get IDailyTrigger
instead of ITimeTrigger
.
See this MSDN page .
a source to share