Page 1 of 1

Task status not tsRunning when started 'manually'

Posted: Tue Jul 06, 2004 6:45 am
by someguy
I am trying to use the Task Scheduler component to create then immediately start (.Run) a task remotely.

The task starts correctly and appears as 'Running' in the Windows Schduler but the component's Status is still shows tsNotScheduled.

I've tried creating a dummy 'Once off' trigger set to 10 years in the future but this didn't fix the problem.

Is there a way to get the 'true' status of a task started in this way?

If it helps, here's a sample of the code being used...

Code: Select all

FTaskScheduler := TTaskScheduler.Create(nil);
FTaskScheduler.TargetMachine := 'THOR';

if not FTaskScheduler.StartScheduler then
     raise Exception.Create( 'Failed.' );

FTaskScheduler.Open();

FRemoteTask := FTaskScheduler.CreateNewItem( ... );
FRemoteTask.ApplicationName := ....
FRemoteTask.SetAccountInformation( ... );    

FDummyTrigger := FRemoteTask.Triggers.Add();
FDummyTrigger.BeginDate := IncYear(Now(),10);
FDummyTrigger.HasEndDate:= false;
FDummyTrigger.TriggerType := ttOnce;
FDummyTrigger.Enabled := true;

FRemoteTask.Save;
FRemoteTask.Activate;

// Run the remote task
//
FRemoteTask.Run;

timeout := IncSecond(Now(), 10);

while (now() <= timeout) and (FRemoteTask.Status <> tsRunning) do
         SleepEx(20, true);

if FRemoteTask.Status <> tsRunning then
      raise Exception.Create( 'Task did not start within timeout' );

...
...

Posted: Tue Jul 06, 2004 4:17 pm
by isiticov
The solution is: you need to call Activate method in case you're supposing that any properties could be changed before this. Activate refreshes the values of all properties. So your code could be modified as following:

Code: Select all

while (now() <= timeout) and (FRemoteTask.Status <> tsRunning) do
begin 
    FRemoteTask.Activate;
    Application.ProcessMessages;
end;
if FRemoteTask.Status <> tsRunning then 
      raise Exception.Create( 'Task did not start within timeout' ); 
P.S. In order to to run dummy tasks you don't need to have any dummy triggers.

Posted: Wed Jul 07, 2004 11:05 am
by someguy
Thanks Igor, that works a treat.

Cheers!