類TThread 方法 WaitFor , 其作用是: 當一個線程必須要等其它線程或進程完成某些任務時 , 可以使用 WaitFor 將線程臨時 Suspend.
1. 等待一個線程完成:
if ListFillingThread.WaitFor then
begin
with ThreadList1.LockList do
begin
for I := 0 to Count - 1 do
ProcessItem(Items[I]);
end;
ThreadList1.UnlockList;
end;
2. 等待一個任務完成:
Event1.ResetEvent; // 清除所有Event;
for i := 1 to Counter do
TaskThread.Create(False); { create and launch task threads }
if Event1.WaitFor(20000) <> wrSignaled then
raise Exception;
// 所有任務線程已經完成, 回到主線程.