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

Suggestion for Task.LastTaskResult

Oct 18, 2010 at 4:17 PM

I was looking for what values can be returned through the LastTaskResult property, but I found two strange sets of error code for v1 and v2 correspondingly: IScheduledWorkItem::GetExitCode and IRegisteredTask::LastTaskResult, which actually didn't help me at all.

Later I found Task Scheduler Error and Success Constants. Unfortunately it looks like it is applicable only for Task Scheduler v2.0.

Will you include it in next TaskScheduler library releases? I've used it at my side, but probably it could be useful for others. 

And don't you know similar codes for Task Scheduler 1.0?

Best regards,

Sergey

Coordinator
Oct 18, 2010 at 10:11 PM

Those constants are typically used as HResults when the native COM library methods fail or succeed. Since those codes are part of the standard error codes supported by .NET, the COMExcpetion will pull that code and try and find the localized text for the error.

Where are you needing these values? If they are useful in a specific use case, I would love to design for it and incorporate it. The LastTaskResult property is the exit code from the last running of an ExecAction.

Oct 19, 2010 at 7:27 AM

Sorry, you are right. I got this code: SCHED_S_TASK_TERMINATED (0x00041306): The last run of the task was terminated by the user.

as a LastTaskResult and thought other values could be useful. So yes, these values are not very helpful.

But how Task Scheduler in Windows does process return codes? It diplays both: message and code. Are they hardcoded in Windows? :)

Coordinator
Oct 19, 2010 at 6:38 PM

Those codes are hardcoded and I have seen them appear usually as the error code within a COMException. Your example of those codes being returned by LastTaskResult is new to me. The textual message associated with each code is also hardcoded and is part of the localized error resources within Windows.

Dec 19, 2015 at 10:33 PM
From all the messages discussing Task.LastTaskResult, this thread seems to be the most detailed. But it's not very clear.

This library returns an integer (without much meaning) as the value for Task.LastTaskResult. Yet Windows task scheduler is able to display a meaningful message such as the ones shown in this image. Also, the Microsoft documentation for RegisteredTask.LastTaskResult property indicates it as a STRING property.

Can you clarify why the wrapper library can't provide the same useful information that the Windows Task Scheduler is providing for the Last Task Result?
Coordinator
Dec 21, 2015 at 6:10 PM
Edited Dec 21, 2015 at 6:13 PM
The Scripting library referenced translates the HRESULT value to a string. The COM library (which is what is used for this wrapper) returns an HRESULT as stated in earlier posts (https://msdn.microsoft.com/en-us/library/windows/desktop/aa380762(v=vs.85).aspx). You can call Marshal.GetExceptionForHR and then use the Message property to get the translated system description for that HRESULT.
string errorMessage = Marshal.GetExceptionForHR(task.LastTaskResult).Message;
Marked as answer by dahall on 12/21/2015 at 10:13 AM
Dec 22, 2015 at 12:09 AM
Thanks.