Cast exception when trying to create new Task Schedueler task.
Posted
by seaneshbaugh
on Stack Overflow
See other posts from Stack Overflow
or by seaneshbaugh
Published on 2010-05-12T11:57:00Z
Indexed on
2010/05/12
12:14 UTC
Read the original article
Hit count: 614
c#
|com-interop
I'm attempting to create a new task in the Windows Task Scheduler in C#. What I've got so far is pretty much a copy and paste of 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 come run time I get the following exception:
Unable to cast COM object of type 'System.__ComObject' to interface type 'TaskScheduler.ITimeTrigger'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B45747E0-EBA7-4276-9F29-85C5BB300006}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Here's all the code so you can see what I'm doing here without following the above link.
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 is throwing the exception is this one
ITimeTrigger Trigger = (ITimeTrigger)Task.Triggers.Create(_TASK_TRIGGER_TYPE2.TASK_TRIGGER_DAILY);
The exception message kinda makes sense to me, but I'm afraid I don't know enough about COM to really know where to begin with this.
Also, I should add that I'm using VS 2010 and I had to set the project to either be for x86 or x64 CPU's instead of the usual "Any CPU" because it kept giving me a BadImageFormatException. I doubt that's related to my current problem, but just in case I thought I might as well mention it.
© Stack Overflow or respective owner