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.
Note You can also create a task by calling ITaskScheduler::AddWorkItem. When you take this route, it is your responsibility to create an instance of the Task object (which supports the ITask interface) and then add the task with the name you supply.
Note By default, only a member of the Administrators, Backup Operators, or Server Operators group can create tasks on Windows Server 2003. A member of the Administrators group may change the security descriptor of the Windows\Task folder to let others create tasks.
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|