Task status not tsRunning when started 'manually'

This forum is designated to discuss SiComponents Scheduling Agent.
Post Reply
someguy
Posts: 6
Joined: Sun May 30, 2004 9:16 am

Task status not tsRunning when started 'manually'

Post 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' );

...
...
isiticov
Site Admin
Posts: 2395
Joined: Thu Nov 21, 2002 3:17 pm

Post 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.
someguy
Posts: 6
Joined: Sun May 30, 2004 9:16 am

Post by someguy »

Thanks Igor, that works a treat.

Cheers!
Post Reply