This project has moved. For the latest updates, please go here.

ComHandlerAction Example

Jul 1, 2009 at 6:43 PM

Can you post an example of how to using the ComHandlerAction, including an implementation of the ITaskHandler COM interface.

Thanks

Jul 4, 2009 at 2:52 PM

I am interested to use this library in my application for firing an event at a certain scheduled time. In this scenario I suppose that ComHandlerAction is good for me but I have no example how to use it.  Besides surely the scheduling code will be implemented within a thread so it is important the library to be thread-safe. Have you never used the library in this context ?

Thanks

Coordinator
Jul 8, 2009 at 8:10 PM

This library wraps (hides) the ITaskScheduler COM interface. The ComHandlerAction class wraps the functionality of the IComHandlerAction interface in the Task Scheduler 2.0 library. It effectively loads the specified COM object and executes methods found in the mandatory exposed interface ITaskHandler. For more information, go to http://msdn.microsoft.com/en-us/library/aa380613(VS.85).aspx. To fire an event at a certain scheduled time you could create a COM object that exposes an ITaskHandler interface and set the GUID for the COM object in the ComHandlerAction.ClassId property. You then need to create the appropriate trigger to set the time to call the object. (See example code on home page and in other discussion threads.)

As far as thread safety, I don't know as I've never tried it. To my recollection, there isn't much that would be affected if you are creating the instances of the TaskService and TaskDefinition within the same thread. If you try it and have problems, be sure to post an issue.

Coordinator
Jul 9, 2009 at 12:18 AM

This thread peaked my interest. I spent a few hours today figuring out how to create a COM library in C# that can be executed using the COMHandlerAction from the Task Scheduler. The sample project is now in the Downloads section.

Jul 24, 2010 at 5:34 PM

Thank you for supplying this interesting example.

When starting your ComHandler task I get the error:
Task Scheduler failed to launch action "" in instance "{b92b6d19-3e39-4561-a700-4309f8d8be28}" of task "\Meinl\TestComHandler". Additional Data: Error Value: 2147746132.
Last Run Result: 0x80040154

Which seems to mean "class not registered".

What am I doing wrong?

Here is what I did:

1. Built your task in VS as admin
2. Ran the COMTask.reg file (had to convert it from UTF-8 to ASCII first)
3. Created a task via the Task Scheduler UI using the following TestComHandler.XML file
with:
  <Actions Context="Author">
    <ComHandler>
        <ClassId>{CE7D4428-8A77-4c5d-8A13-5CAB5D1EC734}</ClassId>
    </ComHandler>
  </Actions>


<?xml version="1.0" encoding="UTF-16"?>
<Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
  <RegistrationInfo>
    <Date>2010-07-24T18:22:22.5119635</Date>
    <Author>zulu\pm</Author>
  </RegistrationInfo>
  <Triggers>
    <CalendarTrigger>
      <StartBoundary>2010-07-24T18:21:54.3523529</StartBoundary>
      <Enabled>true</Enabled>
      <ScheduleByDay>
        <DaysInterval>1</DaysInterval>
      </ScheduleByDay>
    </CalendarTrigger>
  </Triggers>
  <Principals>
    <Principal id="Author">
      <UserId>zulu\pm</UserId>
      <LogonType>InteractiveToken</LogonType>
      <RunLevel>LeastPrivilege</RunLevel>
    </Principal>
  </Principals>
  <Settings>
    <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
    <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
    <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
    <AllowHardTerminate>true</AllowHardTerminate>
    <StartWhenAvailable>false</StartWhenAvailable>
    <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
    <IdleSettings>
      <Duration>PT10M</Duration>
      <WaitTimeout>PT1H</WaitTimeout>
      <StopOnIdleEnd>true</StopOnIdleEnd>
      <RestartOnIdle>false</RestartOnIdle>
    </IdleSettings>
    <AllowStartOnDemand>true</AllowStartOnDemand>
    <Enabled>true</Enabled>
    <Hidden>false</Hidden>
    <RunOnlyIfIdle>false</RunOnlyIfIdle>
    <WakeToRun>false</WakeToRun>
    <ExecutionTimeLimit>P3D</ExecutionTimeLimit>
    <Priority>7</Priority>
  </Settings>
  <Actions Context="Author">
    <ComHandler>
        <ClassId>{CE7D4428-8A77-4c5d-8A13-5CAB5D1EC734}</ClassId>
    </ComHandler>
  </Actions>
</Task>

Coordinator
Aug 2, 2010 at 5:00 PM

I just took my code which worked under XP and Vista and am seeing failures under Win7. I will post my solution once I work through the problem.

Coordinator
Jan 7, 2013 at 5:18 PM

This is now working and is available under the Downloads area.

Apr 15, 2014 at 2:15 PM
Error: Task Scheduler failed to launch action "" ..................................

I found one source of this error. I mean the reason why "" is blank and not appearing with task name.

Possible Reason: The assembly is not registered for COM or Not made as COM visible.

Steps:
  1. Go to the properties window of the your project where the COM Handler is registered.
  2. Click on 'Assembly Information' button.
  3. A checkbox appears which indicates "Mark as COM Visible". Check this.
I guess, some part of this info is provided by Mr. Dahall but I just wanted to share so that you guys can recheck this setting. Thanks.

CHeers,
Jojo
Apr 16, 2014 at 11:58 AM
Solution to Error:

Task Scheduler failed to launch action "..NotificationCOMService" in instance "{d9fa820c-da53-4ed3-8fc4-4df1e90ef1e1}" of task "\NotiFicaTions". Additional Data: Error Value: 2148734217.

It needs a setting in Assembly properties:
  1. Select your project which has above COMHandler class.
  2. Go to properties. Then click on "Build" tab on left.
  3. At bottom, there is checkbox called - Register for COM interop. Check it.
Now compile the assembly and run the code. Hope your error gets resolved.
Marked as answer by dahall on 4/23/2014 at 8:56 PM