Creating a Task Using NewWorkItem Example
When creating a task, you will use two Task Scheduler interfaces: ITaskScheduler and ITask. You must provide a unique name for the task, the class identifier of the task object, and the interface identifier of ITask. The class identifier and interface identifier are shown in the code example following this topic.
The name you supply for the task must be unique within the Scheduled Tasks folder. If a task with the same name already exists, ITaskScheduler::NewWorkItem returns ERROR_FILE_EXISTS. If you get this return value, you should specify a different name and attempt to create the task again.
The following procedure describes how to create a new work item task.
To create a new work item task
- Call CoInitialize to initialize the COM library and CoCreateInstance to get a Task Scheduler object. (This example assumes that the Task Scheduler service is running.)
- Call ITaskScheduler::NewWorkItem to create a new task. (This method returns a pointer to an ITask interface.)
- Save the new task to disk by calling IPersistFile::Save. (The IPersistFile interface is a standard COM interface supported by the ITask interface.)
- Call ITask::Release to release all resources. (Note that Release is an IUnknown method inherited by ITask.)
|For a code example of||See|
|Creating a single task||C/C++ Code Example: Creating a Task Using NewWorkItem|