1

Closed

COMException with RunningTask.Refresh()

description

I want check task state and i write code
RunningTask rt = task.Run();
do
{
    Thread.Sleep(1000);
} while (rt.State == TaskState.Running);
However, I have infinite loop, because state is not updated (running always). When I change cycle body to
do
{
    Thread.Sleep(1000);
    rt.Refresh(); // COMException here
} while (rt.State == TaskState.Running);
Code fires exception if state change from Running to Unknown.

It is correct behaviour?

P.S. In the end, i write first cycle version with
while (task.State == TaskState.Running);
and I do not have problems.
Closed Jul 12 at 10:39 AM by dahall

comments

dahall wrote Jul 5 at 2:14 PM

Please provide the details of the COMException. I suspect that in the second you are waiting, the task ends, changes it state, and then invalidates the COM object interface pointer, thus causing the exception.

Another option is to use TaskEventWatcher.

_r_m_ wrote Jul 6 at 10:39 AM

An unhandled exception of type 'System.Runtime.InteropServices.COMException' occurred in Microsoft.Win32.TaskScheduler.dll
HRESULT: 0x8004130B

Thanks for TaskEventWatcher, I will try it.

OT: How soon you move this project on github?

dahall wrote Jul 6 at 12:43 PM

As I suspected that error code is SCHED_E_TASK_NOT_RUNNING: There is no running instance of the task. That confirms my suspicion I shared with you. I think you are much safer using TaskEventWatcher. There is an example in the Documentation area for using it.

I have already started moving to GitHub (https://github.com/dahall/TaskScheduler). The code is there as is most of the documentation. I haven't closed this site yet as I haven't found a solution for migrating the Discussions area.

wrote Jul 12 at 10:39 AM