I have an application that launches a number threads to do stuff. I have a TThreadOnTerminate procedure for each of the threads that accesses some thread variables to populate a grid with the results. (Each thread performs a different task but the answer is always the same, ie. Success or Fail with a StringList of messages.)
So I have:
procedure TFormMain.Thread1OnTerminate(Sender: TObject);
begin
Result := TThread1(Sender).Result;
AddMessagesToGrid(TThread1(Sender).Messages);
end;
procedure TFormMain.Thread2OnTerminate(Sender: TObject);
begin
Result := TThread2(Sender).Result;
AddMessagesToGrid(TThread2(Sender).Messages);
end;
My question is the following. Can I have a ‘common’ OnTerminate procedure to handle the results of all my threads, as below?
procedure TFormMain.Thread1OnTerminate(Sender: TObject);
begin
Result := <Sender Thread>.Result;
AddMessagesToGrid(<Sender Thread>.Messages);
end;
We currently use Delphi 2007. Soon to be upgraded (I hope) to Delphi XE.
Yes, no reason why you couldn’t, provided all threads implement the same interface, to return the Result value and the messages.
The easiest way to do that is to make an base thread class with the result and messages and make all your current threads descendants of that base class.
and then use it as follows: