I have been following the progress of OTL and finally decided to get my feet wet with threading. I created a simple example that uses ADO and followed all the rules about creating the connection inside the thread. That works fine. However, the problem I am having is when the "go" button is pressed, it falls right through the test of GlobalOmniThreadPool.CountExecuting. If the worker threads are still hanging around and I press the "go" button again, it works as it should and waits till the two tasks are complete before passing. If I wait for the worker threads to expire and then press "go", it falls right past the test again.
Here is the code:
procedure TCCC100.LogMessages(s : string);
begin
Memo1.Lines.Add(s);
end;
procedure TCCC100.LoadInventoryRecords(const task : IOmniTask);
begin
task.Comm.Send(1);
Sleep(500);
end;
procedure TCCC100.LoadPartRecords(const task : IOmniTask);
begin
task.Comm.Send(1);
Sleep(500);
end;
procedure TCCC100.Button1Click(Sender : TObject);
const
MSG_START = 1;
begin
Button1.Enabled := false;
Memo1.Clear;
Memo1.Update;
CreateTask(LoadInventoryRecords, 'LoadInventoryRecords')
.OnMessage(
procedure(const task : IOmniTaskControl; const msg : TOmniMessage)
var taskNumber : Integer;
begin
if msg.MsgID = MSG_START then
begin
taskNumber := task.Param['TaskNumber'].AsInteger;
LogMessages(Format('task Loading Inventory - %d / %d start', [taskNumber, task.UniqueID]));
end;
end)
.OnTerminated(
procedure(const task : IOmniTaskControl)
begin
LogMessages(Format('%d Inventory records loaded.', [0{RS_Inventory.RecordCount}]));
end)
.SetParameter('TaskNumber', 1)
.Schedule(GlobalOmniThreadPool);
Application.ProcessMessages;
CreateTask(LoadPartRecords, 'LoadPartRecords')
.OnMessage(
procedure(const task : IOmniTaskControl; const msg : TOmniMessage)
var taskNumber : Integer;
begin
if msg.MsgID = MSG_START then
begin
taskNumber := task.Param['TaskNumber'].AsInteger;
LogMessages(Format('task Loading Parts - %d / %d start', [taskNumber, task.UniqueID]));
end;
end)
.OnTerminated(
procedure(const task : IOmniTaskControl)
begin
LogMessages(Format('%d part records loaded.', [0{RS_Part.RecordCount}]));
end)
.SetParameter('TaskNumber', 2)
.Schedule(GlobalOmniThreadPool);
Application.ProcessMessages;
//wait all finished
while GlobalOmniThreadPool.CountExecuting + GlobalOmniThreadPool.CountQueued > 0 do
Application.ProcessMessages;
//all task completed
LogMessages('ALL DONE');
Button1.Enabled := true;
end;
Did I not initializes something correctly?
Thank you in advance.
John