In Dephi, I create a thread, like this, which will send message to main form from time to time
Procedure TMyThread.SendLog(I: Integer);
Var
Log: array[0..255] of Char;
Begin
strcopy(@Log,PChar('Log: current stag is ' + IntToStr(I)));
PostMessage(Form1.Handle,WM_UPDATEDATA,Integer(PChar(@Log)),0);
End;
procedure TMyThread.Execute;
var
I: Integer;
begin
for I := 0 to 1024 * 65536 do
begin
if (I mod 65536) == 0 then
begin
SendLog(I);
End;
End;
end;
where WM_UPDATEDATA is a custom message, defined below:
const
WM_UPDATEDATA = WM_USER + 100;
And in main form, it will do as follows to update the list:
procedure TForm1.WMUpdateData(var msg : TMessage);
begin
List1.Items.Add(PChar(msg.WParam));
end;
However, as the Log string sent to the main form is a local variable, which will be destroyed after calling SendLog. While TForm1.WMUpdateData process the message asynchronously, so it is possible that when it is invoked, the Log string has already been destroyed. How to solve this problem?
I think maybe I can allocate the string space in a global system space, and then pass it to the message, then after TForm1.WMUpdateData processes the message, it can destroy the string space in the global space. Is that a workable solution? How to implement this?
Thanks