Multi-threaded in Delphi using message to achieve VCL data synchronization display Lanno Ckeeke2006-5- AOverview: Delphi strictly distinguish between the main thread and the main thread, the main thread is responsible for the GUI update, the child thread is responsible for the data operation, when the data is finished, the child thread can send a message to the main program to notify it to update the data in the VCL. Implementation: The key is to send and receive messages. The wparam and lparam types are longint in the message structure tmessage, and the pointer type is also defined as Longint, which allows you to pass the data of interest to you. such as passing character arrays: Array definitions:ConstMax_len=260; SzName:Array[0.. Max_len] ofChar; Message send: PostMessage (Form1.handle,wm_updatedata,integer (PChar (@szName)),0); How to receive messages:procedureTform1.wmupdatedata (varmsg:tmessage);beginSelf . SHOWDATA.ITEMS.ADD (PChar (Msg. WParam));End; Two data types are defined in a child thread: PublicSzName:Array[0.. Max_len] ofChar; Nindex:integer; Full code: Sub-Threading class:UnitTchildthread;Interface usesclasses,messages,windows,sysutils;//Add a usage file ConstMax_len =260;typetchildthreads=class(TThread)Private {Private Declarations} protected procedureExecute;Override; PublicSzName:Array[0.. Max_len] ofChar; Nindex:integer; End; ImplementationusesUnit1;{Important:methods and properties of objects in VCL or CLX can is only is used in a method called using Synchronize, for Example, Synchronize (updatecaption); And Updatecaption could look like, procedure tchildthread.updatecaption; Begin form1.caption: = ' Updated in a thread '; End } {Tchildthread} procedureTchildthreads.execute;begin {Place Thread code here}PostMessage (Form1.handle,wm_updatedata,integer (PChar (@szName)),0); //note the wording of the Type conversion sectionEnd; End. Main thread code:UnitUnit1;Interface usesWindows, Messages, sysutils, variants, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls, Comctrls;ConstWm_updatedata= Wm_user + -;//custom messages that use this message to implement updates to the GUItypeTForm1=class(tform) Controlpannel:tgroupbox; Startthreads:tbutton; Tabcontrol1:ttabcontrol; Singlethread:tradiobutton; Multiplethreads:tradiobutton; Stopthreads:tbutton; Showdata:tlistbox; Showerror:tlistbox; Initialize:tbutton; Cleanup:tbutton;//GUI updated Message handler function declaration procedureWmupdatedata (varMsg:tmessage);messageWm_updatedata; procedureStartthreadsclick (sender:tobject);Private {Private Declarations} Public {Public Declarations} End; varForm1:tform1;ImplementationusesTchildthread;{$R *.DFM}//message function DefinitionprocedureTform1.wmupdatedata (varmsg:tmessage);beginSelf . SHOWDATA.ITEMS.ADD (PChar (Msg. WParam));End; procedureTform1.startthreadsclick (sender:tobject);varOchildthread:Array[0.. +] oftchildthreads; i:integer;beginFor I:=0 to + Do beginOchildthread[i]:=Tchildthreads.create (TRUE); //data sent to the VCLstrcopy (@oChildThread [I].szname,pchar (' Child'+IntToStr (i))); Ochildthread[i].nindex:=i; Ochildthread[i]. Resume; End; End; End.
Multi-thread in Delphi using message to realize the data synchronization display of VCL