In Dephi, I create a thread, for example, that will occasionally send a message to the main form
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 the custom message defined below:
const WM_UPDATEDATA = WM_USER + 100;
And in the main form, it will update the list as follows:
procedure TForm1.WMUpdateData(var msg : TMessage); begin List1.Items.Add(PChar(msg.WParam)); end;
However, since the log line sent to the main form is a local variable that will be destroyed after the SendLog call. Although TForm1.WMUpdateData processes the message asynchronously, it is possible that when it was called, the log line was already destroyed. How to solve this problem?
I think that maybe I can allocate space in the global system space and then pass it into a message, after TForm1.WMUpdateData processes this message, it can destroy the string space in the global space. Is this an acceptable solution? How to implement this?
thanks
multithreading delphi message postmessage
alancc
source share