Well perhaps I am missing something here. It wouldn't be the first time. It seems to me though I could do this with just TThread, like this:
type
TTextReadyEvent = procedure (sender : TObject; Text : TStrings) of object;
TTextSetter = class(TThread)
private
FOnTextReady: TTextReadyEvent;
protected
FResult : TStrings;
procedure Showresult;
public
procedure Execute; override;
property OnTextReady : TTextReadyEvent read FOnTextReady write FOnTextReady;
end;
procedure TTextSetter.Execute;
begin
FResult := TStringList.Create;
try
BackgroundTask(FResult);
synchronize(Showresult);
finally
FResult.free;
end;
end;
procedure TTextSetter.Showresult;
begin
if assigned(FOnTextReady) then
FOnTextReady(Self, FResult);
end;
procedure TForm3.AddNewText(Sender: TObject; Newtext: TStrings);
begin
Memo1.Lines.AddStrings(Newtext);
end;
procedure TForm3.Button1Click(Sender: TObject);
var
Background : TTextSetter;
begin
Background := TTextSetter.Create(TRUE);
Background.FreeOnTerminate := TRUE;
Background.OnTextReady := AddNewText;
Background.Start;
end;
This runs BackgroundTask in a background thread to prepare the text, updates the GUI in a safe way, and I don't have to abandon the try...finally to do it. So what am I missing?