I am sending a string from an anonymous thread to the UI with PostMessage in the following code:
unit Unit2;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
const
WM_SETCAPTION = WM_USER;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
procedure WMSetCaption(var msg: TMessage); message WM_SETCAPTION;
end;
var
Form2: TForm2;
implementation
{$R *.dfm}
procedure TForm2.WMSetCaption(var msg: TMessage);
begin
Self.Caption := PChar(msg.LParam);
end;
procedure TForm2.Button1Click(Sender: TObject);
begin
System.Classes.TThread.CreateAnonymousThread(
procedure
begin
PostMessage(Handle, WM_SETCAPTION, 0, LParam(PChar('My new caption')));
end).Start;
end;
end.
This works seemingly well, but could this potentially create memory leaks? Or is there a better way to accomplish this?
PostMessagein the above way could cause any problems whatsoever (e.g. memory leaks). \$\endgroup\$FreeOnTerminate, so it will Free itself. \$\endgroup\$