问题
The code below executes without any error, but task didn't executed and I didn't see it in control panel . Please help
HRESULT CreateOnceActionTask(const wchar_t* taskName, const wchar_t* programPath, const wchar_t* params)
{
HRESULT hr = S_OK;
CComPtr<ITaskScheduler> taskScheduler;
if(FAILED(hr = CoCreateInstance(CLSID_CTaskScheduler, NULL, CLSCTX_INPROC_SERVER, IID_ITaskScheduler, (void**)&taskScheduler)))
ReturnErrorMsg(hr, L"Failed to get IID_ITaskScheduler");
// create task
CComPtr<ITask> task;
if(FAILED(hr = taskScheduler->NewWorkItem(taskName, CLSID_CTask, IID_ITask, (IUnknown**)&task)))
{
//if(FAILED(hr = taskScheduler->Activate(taskName, IID_ITask, (IUnknown**) &task)))
ReturnErrorMsg(hr, L"Failed to create task");
//taskScheduler->Delete(taskName);
//return S_OK;
}
if(FAILED(hr = task->SetApplicationName(programPath)))
ReturnErrorMsg(hr, L"Failed to set SetApplicationName");
if(FAILED(hr = task->SetParameters(params)))
ReturnErrorMsg(hr, L"Failed to set SetParameters");
if(FAILED(hr = task->SetTaskFlags(TASK_FLAG_DELETE_WHEN_DONE)))
ReturnErrorMsg(hr, L"Failed to set task flags");
// save task
CComPtr<IPersistFile> persistTask;
if(FAILED(hr = task->QueryInterface(IID_IPersistFile, (void **)&persistTask)))
ReturnErrorMsg(hr, L"Failed to save task with IPersistFile");
if(FAILED(hr = persistTask->Save(NULL,TRUE)))
ReturnErrorMsg(hr, L"Failed to save task with IPersistFile");
// create trigger
CComPtr<ITaskTrigger> taskTrigger;
CComPtr<IPersistFile> persistTrigger = NULL;
WORD wTrigNumber = 0;
if (FAILED(hr = task->CreateTrigger(&wTrigNumber, &taskTrigger)))
ReturnErrorMsg(hr, L"Failed to createTrigger");
// create trigger description
TASK_TRIGGER triggerStruct = {0};
ZeroMemory ( &triggerStruct, sizeof (TASK_TRIGGER) );
triggerStruct.cbTriggerSize = sizeof (TASK_TRIGGER);
triggerStruct.TriggerType = TASK_TIME_TRIGGER_ONCE;
// 10 minutes from now
COleDateTime oDate = COleDateTime::GetCurrentTime() + COleDateTimeSpan(0, 0, 1, 0);
triggerStruct.wBeginYear = oDate.GetYear();
triggerStruct.wBeginMonth = oDate.GetMonth();
triggerStruct.wBeginDay = oDate.GetDay();
triggerStruct.wStartHour = oDate.GetHour();
triggerStruct.wStartMinute = oDate.GetMinute();
// associate the trigger with the task
if (FAILED(hr = taskTrigger->SetTrigger(&triggerStruct)))
ReturnErrorMsg(hr, L"Failed to set trigger for the task");
if (FAILED(hr = task->QueryInterface(IID_IPersistFile, (void **) &persistTrigger)))
ReturnErrorMsg(hr, L"Failed to get IPersistFile for trigger");
if (FAILED(hr = persistTrigger->Save(NULL, FALSE)))
ReturnErrorMsg(hr, L"Failed to save trigger");
if (FAILED(hr = task->Run()))
ReturnErrorMsg(hr, L"Failed to run task");
return S_OK;
}
来源:https://stackoverflow.com/questions/14988449/problems-with-scheduled-task-using-itaskscheduler-itask-interfaces