task.execTask;
state := wtFinished;
task.WorkState := tsFinished;
task.Work := nil;
task.hTask.leave;
//任务完成
if Assigned(fPool.FOnTaskFinished) then
fPool.FOnTaskFinished(task);
end;
finally
end;
end;
end;
WAIT_OBJECT_0 + 1:;//Terminate don't to do something
End;
nc := fPool.TaskQueueCount;
if (nc > 0) then
fpool.PostNewTaskSign
else if (fPool.FAuto) and (fPool.FWaitFlag) then
fPool.RecoverIDle(self);//任务空闲,线程空闲时间大于设定时间时自动回收空闲线程
state := wtIdle;
//将自己移至空闲线程
if not fPool.WaitAutoRecover(self) then //如果当前正在等待自动回收线程的
fPool.MoveQueue(self,1)
else
fPool.Waiting := nil;
end;
end;
procedure TWorkThread.SetDefault;
begin
FState := wtIdle;
end;
{ TWorkTask }
constructor TWorkTask.Create;
begin
hTask := TCriticalSection.Create;
WorkState := tsNone;
FWorkLevel := tlNormal;
Work := nil;
end;
destructor TWorkTask.Destroy;
begin
WorkState := tsFinished;
if Assigned(Work) then
Work.Resume;
hTask.Free;
inherited;
end;
procedure TWorkTask.setWorkState(Const Value:TTaskState);
begin
FWorkState := Value;
case value of
tsReStart:
begin
if Assigned(Work) and (Work.Suspended) then
begin
FWorkState := tsDoing;
Work.Resume;
end;
end;
tsStop:
begin
if Assigned(Work) then
Work.Suspend;
end;
end;
end;
{ TPoolLog }
procedure TPoolLog.OutputLog(const Msg: String; Level: TLogLevel);
begin
// to implement at sub class.
end;
procedure TPoolLog.WriteLog(const Msg: String; Level: TLogLevel);
var
dt:TDatetime;
begin
dt := now;
OutputLog(datetimetostr(dt) + ' : ' + Msg,Level);
end;
end.