You've already forked lazarus-ccr
lazMapViewer: More cosmetic changes.
git-svn-id: https://svn.code.sf.net/p/lazarus-ccr/svn@6314 8e941d3f-bd1b-0410-a28a-d453659cc2b4
This commit is contained in:
@ -59,6 +59,10 @@
|
||||
<UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/>
|
||||
</SearchPaths>
|
||||
<Linking>
|
||||
<Debugging>
|
||||
<UseHeaptrc Value="True"/>
|
||||
<UseExternalDbgSyms Value="True"/>
|
||||
</Debugging>
|
||||
<Options>
|
||||
<Win32>
|
||||
<GraphicApplication Value="True"/>
|
||||
|
@ -257,14 +257,15 @@ begin
|
||||
end;
|
||||
|
||||
procedure TLaunchDownloadJob.ExecuteTask(aTask: integer; FromWaiting: boolean);
|
||||
var iTile : integer;
|
||||
var
|
||||
iTile: integer;
|
||||
begin
|
||||
iTile := aTask - 1;
|
||||
Queue.AddUniqueJob(TEventJob.Create
|
||||
(
|
||||
@Engine.evDownload,
|
||||
TEnvTile.Create(FTiles[iTile], Win),
|
||||
false,
|
||||
false, // owns data
|
||||
Engine.GetTileName(FTiles[iTile])
|
||||
),
|
||||
Launcher
|
||||
|
@ -24,11 +24,12 @@ interface
|
||||
uses
|
||||
Classes, SysUtils,syncobjs,contnrs,forms;
|
||||
|
||||
const ALL_TASK_COMPLETED = -1;
|
||||
const
|
||||
ALL_TASK_COMPLETED = -1;
|
||||
NO_MORE_TASK = 0;
|
||||
|
||||
type
|
||||
TjobQueue = class;
|
||||
TJobQueue = class;
|
||||
|
||||
{ TJob }
|
||||
|
||||
@ -60,9 +61,9 @@ type
|
||||
|
||||
TJobArray = Array of TJob;
|
||||
|
||||
{ TjobQueue }
|
||||
{ TJobQueue }
|
||||
|
||||
TjobQueue = Class
|
||||
TJobQueue = Class
|
||||
private
|
||||
FMainThreadId : TThreadID;
|
||||
FOnIdle: TNotifyEvent;
|
||||
@ -109,26 +110,26 @@ type
|
||||
|
||||
|
||||
implementation
|
||||
|
||||
const
|
||||
WAIT_TIME = 3000;
|
||||
TERMINATE_TIMEOUT = 1000;
|
||||
|
||||
|
||||
Type
|
||||
type
|
||||
|
||||
{ EWaiting }
|
||||
|
||||
EWaiting = Class(Exception)
|
||||
EWaiting = class(Exception)
|
||||
private
|
||||
FLauncher: TJob;
|
||||
FNewJob: TJob;
|
||||
public
|
||||
constructor Create(launcher : TJob;NewJob : TJob);
|
||||
constructor Create(ALauncher: TJob; ANewJob: TJob);
|
||||
end;
|
||||
|
||||
{ TRestartTask }
|
||||
|
||||
TRestartTask = Class(TJob)
|
||||
TRestartTask = class(TJob)
|
||||
private
|
||||
FStarted: Boolean;
|
||||
FJob: TJob;
|
||||
@ -146,15 +147,41 @@ Type
|
||||
|
||||
{ TQueueThread }
|
||||
|
||||
TQueueThread = Class(TThread)
|
||||
TQueueThread = class(TThread)
|
||||
private
|
||||
MyQueue : TJobqueue;
|
||||
MyQueue: TJobQueue;
|
||||
function ProcessJob: boolean;
|
||||
public
|
||||
constructor Create(aQueue: TJobQueue);
|
||||
procedure Execute; override;
|
||||
end;
|
||||
|
||||
{ TSyncCallData }
|
||||
|
||||
TSyncCallData = Class
|
||||
private
|
||||
FMethod: TDataEvent;
|
||||
FData: PtrInt;
|
||||
public
|
||||
constructor Create(AMethod: TDataEvent; AData: PtrInt);
|
||||
procedure SyncCall;
|
||||
end;
|
||||
|
||||
|
||||
{ TSyncCallData }
|
||||
|
||||
constructor TSyncCallData.Create(AMethod: TDataEvent; AData: PtrInt);
|
||||
begin
|
||||
FMethod := AMethod;
|
||||
FData := AData;
|
||||
end;
|
||||
|
||||
procedure TSyncCallData.SyncCall;
|
||||
begin
|
||||
FMethod(FData);
|
||||
end;
|
||||
|
||||
|
||||
{ TRestartTask }
|
||||
|
||||
procedure TRestartTask.DoCancel;
|
||||
@ -193,76 +220,21 @@ end;
|
||||
|
||||
function TRestartTask.Running: boolean;
|
||||
begin
|
||||
Result:=Fstarted;
|
||||
Result := FStarted;
|
||||
end;
|
||||
|
||||
|
||||
{ EWaiting }
|
||||
|
||||
constructor EWaiting.Create(launcher: TJob; NewJob: TJob);
|
||||
constructor EWaiting.Create(ALauncher: TJob; ANewJob: TJob);
|
||||
begin
|
||||
FLauncher:=launcher;
|
||||
FNewJob:=NewJob;
|
||||
FLauncher := ALauncher;
|
||||
FNewJob := ANewJob;
|
||||
end;
|
||||
|
||||
|
||||
{ TQueueThread }
|
||||
|
||||
function TQueueThread.ProcessJob : boolean;
|
||||
var aJob : TJob;
|
||||
TaskId : Integer;
|
||||
|
||||
Procedure SetRes(e : Exception);
|
||||
Begin
|
||||
MyQueue.EnterCriticalSection;
|
||||
Try
|
||||
MyQueue.pTaskEnded(aJob,TaskId,nil);
|
||||
finally
|
||||
MyQueue.LeaveCriticalSection;
|
||||
end;
|
||||
end;
|
||||
var RestartTask : boolean;
|
||||
SomeJob : Boolean;
|
||||
begin
|
||||
Result:=false;
|
||||
Repeat
|
||||
SomeJob:=false;
|
||||
MyQueue.EnterCriticalSection;
|
||||
Try
|
||||
result:=result or (MyQueue.Jobs.Count>0);
|
||||
aJob:=MyQueue.pGetJob(TaskId,RestartTask);
|
||||
if Assigned(aJob) then
|
||||
Begin
|
||||
if TaskId=ALL_TASK_COMPLETED then
|
||||
begin
|
||||
MyQueue.pJobCompleted(aJob);
|
||||
SomeJob := true;
|
||||
end
|
||||
else
|
||||
Begin
|
||||
MyQueue.FEvent.ResetEvent;
|
||||
if not(RestartTask) then
|
||||
MyQueue.pTaskStarted(aJob,TaskId);
|
||||
end;
|
||||
end;
|
||||
finally
|
||||
MyQueue.LeaveCriticalSection;
|
||||
end;
|
||||
if Assigned(aJob) then
|
||||
Begin
|
||||
SomeJob:=true;
|
||||
Try
|
||||
aJob.ExecuteTask(TaskId,RestartTask);
|
||||
SetRes(nil);
|
||||
Except
|
||||
on e : Exception do
|
||||
if e.InheritsFrom(EWaiting) then
|
||||
MyQueue.DoWaiting(e,TaskId)
|
||||
else
|
||||
SetRes(e);
|
||||
end;
|
||||
end;
|
||||
until SomeJob=false;
|
||||
end;
|
||||
|
||||
constructor TQueueThread.Create(aQueue: TJobQueue);
|
||||
begin
|
||||
MyQueue := aQueue;
|
||||
@ -270,21 +242,22 @@ begin
|
||||
end;
|
||||
|
||||
procedure TQueueThread.Execute;
|
||||
var wRes : TWaitResult;
|
||||
var
|
||||
wRes: TWaitResult;
|
||||
begin
|
||||
while not Terminated do
|
||||
begin
|
||||
wRes := MyQueue.FEvent.WaitFor(WAIT_TIME);
|
||||
if not(Terminated) then
|
||||
Begin
|
||||
if not(ProcessJob) then
|
||||
if not Terminated then
|
||||
begin
|
||||
if not ProcessJob then
|
||||
if wRes = wrTimeout then
|
||||
if Assigned(MyQueue.OnIdle) then
|
||||
MyQueue.OnIdle(self);
|
||||
end;
|
||||
end;
|
||||
MyQueue.EnterCriticalSection;
|
||||
Try
|
||||
try
|
||||
inc(MyQueue.TerminatedThread);
|
||||
if Assigned(MyQueue.TerminateEvent) then
|
||||
if MyQueue.TerminatedThread=MyQueue.Threads.count then
|
||||
@ -294,29 +267,107 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TjobQueue }
|
||||
function TQueueThread.ProcessJob: boolean;
|
||||
var
|
||||
aJob: TJob;
|
||||
TaskId: Integer;
|
||||
|
||||
procedure TjobQueue.SetUseThreads(AValue: boolean);
|
||||
procedure SetRes(e: Exception);
|
||||
begin
|
||||
MyQueue.EnterCriticalSection;
|
||||
try
|
||||
MyQueue.pTaskEnded(aJob,TaskId,nil);
|
||||
finally
|
||||
MyQueue.LeaveCriticalSection;
|
||||
end;
|
||||
end;
|
||||
|
||||
var
|
||||
RestartTask: boolean;
|
||||
SomeJob: Boolean;
|
||||
begin
|
||||
Result := false;
|
||||
Repeat
|
||||
SomeJob := false;
|
||||
MyQueue.EnterCriticalSection;
|
||||
try
|
||||
Result := Result or (MyQueue.Jobs.Count > 0);
|
||||
aJob := MyQueue.pGetJob(TaskId, RestartTask);
|
||||
if Assigned(aJob) then
|
||||
begin
|
||||
if TaskId = ALL_TASK_COMPLETED then
|
||||
begin
|
||||
MyQueue.pJobCompleted(aJob);
|
||||
SomeJob := true;
|
||||
end
|
||||
else
|
||||
begin
|
||||
MyQueue.FEvent.ResetEvent;
|
||||
if not(RestartTask) then
|
||||
MyQueue.pTaskStarted(aJob, TaskId);
|
||||
end;
|
||||
end;
|
||||
finally
|
||||
MyQueue.LeaveCriticalSection;
|
||||
end;
|
||||
if Assigned(aJob) then
|
||||
begin
|
||||
SomeJob := true;
|
||||
try
|
||||
aJob.ExecuteTask(TaskId, RestartTask);
|
||||
SetRes(nil);
|
||||
except
|
||||
on e: Exception do
|
||||
if e.InheritsFrom(EWaiting) then
|
||||
MyQueue.DoWaiting(e, TaskId)
|
||||
else
|
||||
SetRes(e);
|
||||
end;
|
||||
end;
|
||||
until not SomeJob;
|
||||
end;
|
||||
|
||||
|
||||
{ TJobQueue }
|
||||
|
||||
constructor TJobQueue.Create(NbThread: integer);
|
||||
begin
|
||||
waitings := TStringList.Create;
|
||||
FNbThread := NbThread;
|
||||
FMainThreadId := GetCurrentThreadId;
|
||||
end;
|
||||
|
||||
destructor TJobQueue.Destroy;
|
||||
begin
|
||||
FreeThreads;
|
||||
ClearWaitings;
|
||||
FreeAndNil(Waitings);
|
||||
inherited;
|
||||
end;
|
||||
|
||||
procedure TJobQueue.SetUseThreads(AValue: boolean);
|
||||
begin
|
||||
if FUseThreads = AValue then
|
||||
Exit;
|
||||
FUseThreads := AValue;
|
||||
if Fusethreads then
|
||||
if FUsethreads then
|
||||
InitThreads
|
||||
else
|
||||
FreeThreads;
|
||||
end;
|
||||
|
||||
procedure TjobQueue.ClearWaitings;
|
||||
var i : integer;
|
||||
procedure TJobQueue.ClearWaitings;
|
||||
var
|
||||
i: integer;
|
||||
begin
|
||||
For i:=0 to pred(Waitings.count) do
|
||||
for i := 0 to pred(Waitings.count) do
|
||||
Waitings.Objects[i].Free;
|
||||
Waitings.Clear;
|
||||
end;
|
||||
|
||||
procedure TjobQueue.InitThreads;
|
||||
var i : integer;
|
||||
procedure TJobQueue.InitThreads;
|
||||
var
|
||||
i: integer;
|
||||
begin
|
||||
Jobs := TObjectList.Create(true);
|
||||
Threads := TObjectList.Create(true);
|
||||
@ -327,16 +378,17 @@ begin
|
||||
Threads.Add(TQueueThread.Create(self));
|
||||
end;
|
||||
|
||||
procedure TjobQueue.FreeThreads;
|
||||
var i : integer;
|
||||
procedure TJobQueue.FreeThreads;
|
||||
var
|
||||
i: integer;
|
||||
begin
|
||||
if Assigned(Threads) then
|
||||
Begin
|
||||
begin
|
||||
TerminateEvent := TEvent.Create(nil, false, false, '');
|
||||
Try
|
||||
try
|
||||
FEvent.SetEvent;
|
||||
TerminatedThread := 0;
|
||||
For i:=0 to pred(Threads.Count) do
|
||||
for i:=0 to pred(Threads.Count) do
|
||||
TQueueThread(Threads[i]).Terminate;
|
||||
TerminateEvent.WaitFor(TERMINATE_TIMEOUT);
|
||||
FreeAndNil(FSect);
|
||||
@ -349,20 +401,21 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TjobQueue.EnterCriticalSection;
|
||||
procedure TJobQueue.EnterCriticalSection;
|
||||
begin
|
||||
if Assigned(FSect) and UseThreads then
|
||||
FSect.Enter;
|
||||
end;
|
||||
|
||||
procedure TjobQueue.LeaveCriticalSection;
|
||||
procedure TJobQueue.LeaveCriticalSection;
|
||||
begin
|
||||
if Assigned(FSect) and UseThreads then
|
||||
FSect.Leave;
|
||||
end;
|
||||
|
||||
procedure TjobQueue.DoWaiting(E : Exception;TaskId : integer);
|
||||
var we : EWaiting;
|
||||
procedure TJobQueue.DoWaiting(E: Exception; TaskId: integer);
|
||||
var
|
||||
we: EWaiting;
|
||||
begin
|
||||
EnterCriticalSection;
|
||||
try
|
||||
@ -374,21 +427,21 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TjobQueue.pAddWaiting(aJob: TJob; aTask: integer; JobId: String);
|
||||
procedure TJobQueue.pAddWaiting(aJob: TJob; aTask: integer; JobId: String);
|
||||
begin
|
||||
Waitings.AddObject(JobId, TRestartTask.Create(aJob, aTask));
|
||||
end;
|
||||
|
||||
procedure TjobQueue.pTaskStarted(aJob: TJob; aTask: integer);
|
||||
procedure TJobQueue.pTaskStarted(aJob: TJob; aTask: integer);
|
||||
begin
|
||||
aJob.pTaskStarted(aTask);
|
||||
end;
|
||||
|
||||
procedure TjobQueue.pJobCompleted(var aJob: TJob);
|
||||
procedure TJobQueue.pJobCompleted(var aJob: TJob);
|
||||
Begin
|
||||
pNotifyWaitings(aJob);
|
||||
if FuseThreads then
|
||||
Begin
|
||||
begin
|
||||
Jobs.Remove(aJob);
|
||||
aJob := nil;
|
||||
end
|
||||
@ -396,72 +449,74 @@ Begin
|
||||
FreeAndNil(aJob);
|
||||
end;
|
||||
|
||||
procedure TjobQueue.pTaskEnded(var aJob: TJob; aTask: integer; aExcept: Exception);
|
||||
procedure TJobQueue.pTaskEnded(var aJob: TJob; aTask: integer; aExcept: Exception);
|
||||
begin
|
||||
aJob.pTaskEnded(aTask, aExcept);
|
||||
if (aJob.pGetTask = ALL_TASK_COMPLETED) then
|
||||
Begin
|
||||
pJobcompleted(aJob);
|
||||
end;
|
||||
end;
|
||||
|
||||
function TjobQueue.pGetJob(out TaskId : integer;out Restart : boolean): TJob;
|
||||
var iJob : integer;
|
||||
function TJobQueue.pGetJob(out TaskId: integer; out Restart: boolean): TJob;
|
||||
var
|
||||
iJob: integer;
|
||||
aJob: TJob;
|
||||
begin
|
||||
Restart := false;
|
||||
Result := nil;
|
||||
For iJob:=0 to pred(Jobs.Count) do
|
||||
Begin
|
||||
for iJob := 0 to pred(Jobs.Count) do
|
||||
begin
|
||||
aJob := TJob(Jobs[iJob]);
|
||||
if aJob.InheritsFrom(TRestartTask) then
|
||||
Begin
|
||||
result:=TRestartTask(aJob).FJob;
|
||||
begin
|
||||
Result := TRestartTask(aJob).FJob;
|
||||
TaskId := TRestartTask(aJob).FTask;
|
||||
Restart := true;
|
||||
Jobs.Delete(iJob);
|
||||
Exit;
|
||||
exit;
|
||||
end;
|
||||
TaskId := aJob.pGetTask;
|
||||
if (TaskId>NO_MORE_TASK) or (TaskId=ALL_TASK_COMPLETED) then
|
||||
Begin
|
||||
begin
|
||||
Result := aJob;
|
||||
Exit;
|
||||
end;
|
||||
end;
|
||||
if not(assigned(result)) then
|
||||
if not Assigned(Result) then
|
||||
TaskId := NO_MORE_TASK;
|
||||
end;
|
||||
|
||||
function TjobQueue.pFindJobByName(const aName: string;ByLauncher: TObject): TJobArray;
|
||||
var iRes,i : integer;
|
||||
function TJobQueue.pFindJobByName(const aName: string;
|
||||
ByLauncher: TObject): TJobArray;
|
||||
var
|
||||
iRes, i: integer;
|
||||
begin
|
||||
SetLength(result,Jobs.count);
|
||||
SetLength(Result, Jobs.Count);
|
||||
iRes := 0;
|
||||
For i:=0 to pred(Jobs.Count) do
|
||||
Begin
|
||||
for i := 0 to pred(Jobs.Count) do
|
||||
begin
|
||||
if TJob(Jobs[i]).Name = aName then
|
||||
begin
|
||||
if (ByLauncher = nil) or (TJob(Jobs[i]).FLauncher = ByLauncher) then
|
||||
Begin
|
||||
begin
|
||||
Result[iRes] := TJob(Jobs[i]);
|
||||
inc(iRes);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
SetLength(result,iRes);
|
||||
SetLength(Result, iRes);
|
||||
end;
|
||||
|
||||
procedure TjobQueue.pNotifyWaitings(aJob: TJob);
|
||||
var JobId : String;
|
||||
procedure TJobQueue.pNotifyWaitings(aJob: TJob);
|
||||
var
|
||||
JobId: String;
|
||||
ObjRestart: TRestartTask;
|
||||
idx: integer;
|
||||
begin
|
||||
JobId := aJob.Name;
|
||||
Repeat
|
||||
repeat
|
||||
idx := waitings.IndexOf(JobId);
|
||||
if idx <> -1 then
|
||||
Begin
|
||||
begin
|
||||
ObjRestart := TRestartTask(waitings.Objects[idx]);
|
||||
waitings.Delete(idx);
|
||||
Jobs.Add(ObjRestart);
|
||||
@ -469,27 +524,12 @@ begin
|
||||
until idx = -1;
|
||||
end;
|
||||
|
||||
function TjobQueue.IsMainThread: boolean;
|
||||
function TJobQueue.IsMainThread: boolean;
|
||||
begin
|
||||
Result:=GetCurrentThreadId=FMainThreadID;
|
||||
Result := (GetCurrentThreadId = FMainThreadID);
|
||||
end;
|
||||
|
||||
constructor TjobQueue.Create(NbThread: integer);
|
||||
begin
|
||||
waitings := TStringList.create;
|
||||
FNbThread := NbThread;
|
||||
FMainThreadId := GetCurrentThreadId;
|
||||
end;
|
||||
|
||||
destructor TjobQueue.Destroy;
|
||||
begin
|
||||
FreeThreads;
|
||||
ClearWaitings;
|
||||
FreeAndNil(Waitings);
|
||||
inherited;
|
||||
end;
|
||||
|
||||
procedure TjobQueue.QueueAsyncCall(const AMethod: TDataEvent; Data: PtrInt);
|
||||
procedure TJobQueue.QueueAsyncCall(const AMethod: TDataEvent; Data: PtrInt);
|
||||
begin
|
||||
if UseThreads then
|
||||
Application.QueueAsyncCall(aMethod,Data)
|
||||
@ -497,84 +537,58 @@ begin
|
||||
AMethod(Data);
|
||||
end;
|
||||
|
||||
|
||||
Type
|
||||
|
||||
{ TSyncCallData }
|
||||
|
||||
TSyncCallData = Class
|
||||
private
|
||||
FMethod : TDataEvent;
|
||||
FData : PtrInt;
|
||||
public
|
||||
Constructor Create(AMethod : TDataEvent;AData : PtrInt);
|
||||
Procedure SyncCall;
|
||||
End;
|
||||
|
||||
{ TSyncCallData }
|
||||
|
||||
constructor TSyncCallData.Create(AMethod: TDataEvent; AData: PtrInt);
|
||||
begin
|
||||
FMethod:=AMethod;
|
||||
FData:=AData;
|
||||
|
||||
end;
|
||||
|
||||
procedure TSyncCallData.SyncCall;
|
||||
begin
|
||||
FMethod(FData);
|
||||
end;
|
||||
|
||||
procedure TjobQueue.QueueSyncCall(const AMethod: TDataEvent; Data: PtrInt);
|
||||
var tmp : TSyncCallData;
|
||||
procedure TJobQueue.QueueSyncCall(const AMethod: TDataEvent; Data: PtrInt);
|
||||
var
|
||||
tmp: TSyncCallData;
|
||||
begin
|
||||
tmp := TSyncCallData.Create(AMethod,Data);
|
||||
Try
|
||||
try
|
||||
TThread.Synchronize(nil, @tmp.SyncCall);
|
||||
finally
|
||||
tmp.free;
|
||||
tmp.Free;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TjobQueue.AddJob(aJob: TJob;Launcher : TObject);
|
||||
var TaskId : Integer;
|
||||
procedure TJobQueue.AddJob(aJob: TJob; Launcher: TObject);
|
||||
var
|
||||
TaskId: Integer;
|
||||
restart: boolean;
|
||||
begin
|
||||
aJob.FLauncher := Launcher;
|
||||
aJob.Queue := self;
|
||||
if Usethreads then
|
||||
Begin
|
||||
begin
|
||||
EnterCriticalSection;
|
||||
Try
|
||||
Jobs.add(aJob);
|
||||
try
|
||||
Jobs.Add(aJob);
|
||||
finally
|
||||
LeaveCriticalSection;
|
||||
end;
|
||||
FEvent.SetEvent;
|
||||
end
|
||||
Else
|
||||
Begin
|
||||
Try
|
||||
Repeat
|
||||
else
|
||||
begin
|
||||
try
|
||||
repeat
|
||||
TaskId := aJob.pGetTask;
|
||||
restart := false;
|
||||
if TaskId > NO_MORE_TASK then
|
||||
Begin
|
||||
begin
|
||||
pTaskStarted(aJob, TaskId);
|
||||
Try
|
||||
try
|
||||
aJob.ExecuteTask(TaskId, restart);
|
||||
pTaskEnded(aJob,TaskId, nil);
|
||||
except
|
||||
on e: Exception do
|
||||
Begin
|
||||
if not(e.InheritsFrom(EWaiting)) then
|
||||
begin
|
||||
if not e.InheritsFrom(EWaiting) then
|
||||
pTaskEnded(aJob, TaskId, e)
|
||||
else
|
||||
DoWaiting(e, TaskId);
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
if not(Assigned(aJob)) then
|
||||
if not Assigned(aJob) then
|
||||
TaskId := ALL_TASK_COMPLETED;
|
||||
until TaskId = ALL_TASK_COMPLETED;
|
||||
finally
|
||||
@ -583,19 +597,20 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
function TjobQueue.AddUniqueJob(aJob: TJob; Launcher: TObject): boolean;
|
||||
var lst : TJobArray;
|
||||
function TJobQueue.AddUniqueJob(aJob: TJob; Launcher: TObject): boolean;
|
||||
var
|
||||
lst: TJobArray;
|
||||
begin
|
||||
Result := true;
|
||||
if FUseThreads then
|
||||
Begin
|
||||
begin
|
||||
aJob.Queue := self;
|
||||
aJob.FLauncher := Launcher;
|
||||
EnterCriticalSection;
|
||||
Try
|
||||
try
|
||||
lst := pFindJobByName(aJob.Name, Launcher);
|
||||
if length(lst)=0 then
|
||||
Jobs.add(aJob)
|
||||
if Length(lst) = 0 then
|
||||
Jobs.Add(aJob)
|
||||
else
|
||||
Result := false;
|
||||
finally
|
||||
@ -603,29 +618,30 @@ begin
|
||||
end;
|
||||
FEvent.SetEvent;;
|
||||
end
|
||||
Else
|
||||
else
|
||||
AddJob(aJob,Launcher);
|
||||
end;
|
||||
|
||||
function TjobQueue.CancelAllJob(ByLauncher: TObject) : TJobArray;
|
||||
var i,iJob : integer;
|
||||
function TJobQueue.CancelAllJob(ByLauncher: TObject): TJobArray;
|
||||
var
|
||||
i, iJob: integer;
|
||||
begin
|
||||
SetLength(Result, 0);
|
||||
if FUseThreads then
|
||||
Begin
|
||||
begin
|
||||
EnterCriticalSection;
|
||||
Try
|
||||
try
|
||||
SetLEngth(Result, Jobs.Count);
|
||||
iJob := 0;
|
||||
For i:=pred(Jobs.Count) downto 0 do
|
||||
Begin
|
||||
for i := pred(Jobs.Count) downto 0 do
|
||||
begin
|
||||
if (ByLauncher = nil) or (TJob(Jobs[i]).FLauncher = ByLauncher) then
|
||||
Begin
|
||||
begin
|
||||
TJob(Jobs[i]).Cancel;
|
||||
Result[iJob] := TJob(Jobs[i]);
|
||||
iJob += 1;
|
||||
End;
|
||||
End;
|
||||
end;
|
||||
end;
|
||||
SetLength(Result, iJob);
|
||||
finally
|
||||
LeaveCriticalSection;
|
||||
@ -633,29 +649,31 @@ begin
|
||||
end;
|
||||
end;
|
||||
|
||||
function TjobQueue.CancelJobByName(aJobName: String;ByLauncher: TObject) : boolean;
|
||||
var lst : TJobArray;
|
||||
function TJobQueue.CancelJobByName(aJobName: String; ByLauncher: TObject): boolean;
|
||||
var
|
||||
lst: TJobArray;
|
||||
i: integer;
|
||||
begin
|
||||
Result := false;
|
||||
if FUseThreads then
|
||||
Begin
|
||||
begin
|
||||
EnterCriticalSection;
|
||||
Try
|
||||
try
|
||||
lst := pFindJobByName(aJobName, ByLauncher);
|
||||
For i:=low(lst) to high(lst) do
|
||||
Begin
|
||||
result:=true;
|
||||
for i := Low(lst) to High(lst) do
|
||||
begin
|
||||
Result := true;
|
||||
lst[i].Cancel;
|
||||
End;
|
||||
end;
|
||||
finally
|
||||
LeaveCriticalSection;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TjobQueue.WaitForTerminate(const lstJob: TJobArray);
|
||||
var OneFound : Boolean;
|
||||
procedure TJobQueue.WaitForTerminate(const lstJob: TJobArray);
|
||||
var
|
||||
OneFound: Boolean;
|
||||
i: integer;
|
||||
mThread: Boolean;
|
||||
TimeOut: integer;
|
||||
@ -663,15 +681,15 @@ begin
|
||||
TimeOut := 0;
|
||||
mThread := IsMainThread;
|
||||
if FUseThreads then
|
||||
Begin
|
||||
begin
|
||||
repeat
|
||||
OneFound := False;
|
||||
EnterCriticalSection;
|
||||
Try
|
||||
For i:=low(lstJob) to high(lstJob) do
|
||||
Begin
|
||||
try
|
||||
for i := Low(lstJob) to High(lstJob) do
|
||||
begin
|
||||
if Jobs.IndexOf(lstJob[i]) <> -1 then
|
||||
Begin
|
||||
begin
|
||||
OneFound := True;
|
||||
break;
|
||||
end;
|
||||
@ -680,52 +698,53 @@ begin
|
||||
LeaveCriticalSection;
|
||||
end;
|
||||
if OneFound and (TimeOut > 200) then
|
||||
Raise Exception.Create('TimeOut');
|
||||
raise Exception.Create('TimeOut');
|
||||
if mThread then
|
||||
Application.ProcessMessages;
|
||||
if OneFound then
|
||||
Sleep(100);
|
||||
Inc(TimeOut);
|
||||
until not(OneFound);
|
||||
until not OneFound;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TjobQueue.WaitAllJobTerminated(ByLauncher: TObject);
|
||||
var OneFound : boolean;
|
||||
procedure TJobQueue.WaitAllJobTerminated(ByLauncher: TObject);
|
||||
var
|
||||
OneFound: boolean;
|
||||
i: integer;
|
||||
TimeOut: integer;
|
||||
mThread: Boolean;
|
||||
|
||||
Procedure CheckTimeOut;
|
||||
Begin
|
||||
procedure CheckTimeOut;
|
||||
begin
|
||||
if TimeOut > 200 then
|
||||
Raise Exception.Create('TimeOut');
|
||||
raise Exception.Create('TimeOut');
|
||||
if mThread then
|
||||
Application.ProcessMessages;
|
||||
sleep(100);
|
||||
Sleep(100);
|
||||
inc(TimeOut);
|
||||
end;
|
||||
|
||||
begin
|
||||
TimeOut := 0;
|
||||
if FUseThreads then
|
||||
Begin
|
||||
begin
|
||||
mThread := IsMainThread;
|
||||
if ByLauncher = nil then
|
||||
Begin
|
||||
While Jobs.Count>0 do
|
||||
begin
|
||||
while Jobs.Count > 0 do
|
||||
CheckTimeOut;
|
||||
end
|
||||
else
|
||||
Begin
|
||||
begin
|
||||
repeat
|
||||
OneFound := False;
|
||||
EnterCriticalSection;
|
||||
Try
|
||||
For i:=0 to pred(Jobs.Count) do
|
||||
Begin
|
||||
try
|
||||
for i := 0 to pred(Jobs.Count) do
|
||||
begin
|
||||
if TJob(Jobs[i]).FLauncher = ByLauncher then
|
||||
Begin
|
||||
begin
|
||||
OneFound := True;
|
||||
break;
|
||||
end;
|
||||
@ -735,34 +754,35 @@ begin
|
||||
end;
|
||||
if OneFound then
|
||||
CheckTimeOut;
|
||||
until not(OneFound);
|
||||
until not OneFound;
|
||||
end;
|
||||
end;
|
||||
end;
|
||||
|
||||
{ TjobQueue }
|
||||
{ TJobQueue }
|
||||
|
||||
procedure TJob.Cancel;
|
||||
var lst : Array of TRestartTask;
|
||||
var
|
||||
lst: Array of TRestartTask;
|
||||
i, idx: integer;
|
||||
begin
|
||||
Queue.EnterCriticalSection;
|
||||
Try
|
||||
try
|
||||
FCancelled := true;
|
||||
if (Name<>'') and (Queue.waitings.count>0) then
|
||||
Begin
|
||||
if (Name <> '') and (Queue.waitings.Count > 0) then
|
||||
begin
|
||||
SetLength(lst, 0);
|
||||
Repeat
|
||||
repeat
|
||||
idx := Queue.waitings.IndexOf(Name);
|
||||
if idx <> -1 then
|
||||
Begin
|
||||
SetLength(lst,length(lst)+1);
|
||||
lst[high(lst)]:=TRestartTask(Queue.waitings.Objects[idx]);
|
||||
begin
|
||||
SetLength(lst, Length(lst)+1);
|
||||
lst[High(lst)] := TRestartTask(Queue.waitings.Objects[idx]);
|
||||
Queue.waitings.Delete(idx);
|
||||
end;
|
||||
until idx = -1;
|
||||
For i:=low(lst) to high(lst) do
|
||||
Begin
|
||||
for i := Low(lst) to High(lst) do
|
||||
begin
|
||||
lst[i].Cancel;
|
||||
lst[i].pTaskEnded(1, nil);
|
||||
lst[i].Free;
|
||||
@ -776,7 +796,7 @@ end;
|
||||
|
||||
procedure TJob.DoCancel;
|
||||
begin
|
||||
|
||||
//
|
||||
end;
|
||||
|
||||
function TJob.pGetTask: integer;
|
||||
@ -786,7 +806,7 @@ end;
|
||||
|
||||
procedure TJob.WaitForResultOf(aJob: TJob);
|
||||
begin
|
||||
Raise EWaiting.Create(self,aJob);
|
||||
raise EWaiting.Create(self,aJob);
|
||||
end;
|
||||
|
||||
procedure TJob.EnterCriticalSection;
|
||||
|
@ -26,8 +26,7 @@ uses
|
||||
|
||||
|
||||
type
|
||||
{ TSimpleJob }
|
||||
//job with only one task
|
||||
{ TSimpleJob: job with only one task }
|
||||
TSimpleJob = class(TJob)
|
||||
private
|
||||
FRunning, FEnded: boolean;
|
||||
@ -39,19 +38,19 @@ type
|
||||
function Running : boolean; override;
|
||||
end;
|
||||
|
||||
TJobProc = Procedure (Data : TObject;Job : TJob) of object;
|
||||
TJobProc = procedure (Data: TObject; Job: TJob) of object;
|
||||
|
||||
{ TEventJob }
|
||||
//job with only one task (callback an event)
|
||||
TEventJob = Class(TSimpleJob)
|
||||
{ TEventJob: job with only one task (callback an event) }
|
||||
TEventJob = class(TSimpleJob)
|
||||
private
|
||||
FData: TObject;
|
||||
FTask: TJobProc;
|
||||
FOwnData: Boolean;
|
||||
public
|
||||
constructor Create(aEvent : TJobProc;Data : TObject;OwnData : Boolean;JobName : String='');virtual;
|
||||
procedure ExecuteTask(aTask : integer;FromWaiting : boolean);override;
|
||||
constructor Create(aEvent: TJobProc; Data: TObject; OwnData: Boolean;
|
||||
JobName: String = ''); virtual;
|
||||
destructor Destroy; override;
|
||||
procedure ExecuteTask(aTask: integer; FromWaiting: boolean); override;
|
||||
end;
|
||||
|
||||
|
||||
@ -65,23 +64,17 @@ begin
|
||||
Name := JobName;
|
||||
FTask := aEvent;
|
||||
if Assigned(Data) or OwnData then
|
||||
Begin
|
||||
begin
|
||||
FData := Data;
|
||||
FOwnData := OwnData;
|
||||
end
|
||||
else
|
||||
Begin
|
||||
begin
|
||||
FOwnData := false;
|
||||
FData := self;
|
||||
end;
|
||||
end;
|
||||
|
||||
procedure TEventJob.ExecuteTask(aTask : integer;FromWaiting : boolean);
|
||||
begin
|
||||
if Assigned(FTask) then
|
||||
FTask(FData,self);
|
||||
end;
|
||||
|
||||
destructor TEventJob.Destroy;
|
||||
begin
|
||||
if FOwnData then
|
||||
@ -90,12 +83,19 @@ begin
|
||||
inherited Destroy;
|
||||
end;
|
||||
|
||||
procedure TEventJob.ExecuteTask(aTask: integer; FromWaiting: boolean);
|
||||
begin
|
||||
if Assigned(FTask) then
|
||||
FTask(FData, self);
|
||||
end;
|
||||
|
||||
|
||||
{ TSimpleJob }
|
||||
|
||||
function TSimpleJob.pGetTask: integer;
|
||||
begin
|
||||
if FRunning or Cancelled then
|
||||
Begin
|
||||
begin
|
||||
if not FRunning then
|
||||
Result := ALL_TASK_COMPLETED
|
||||
else
|
||||
|
Reference in New Issue
Block a user