Delphi7 sending messages from a child thread to the main thread triggering event execution

Source: Internet
Author: User
Tags sendmsg

"Reprint" Delphi7 sends a message from a child thread to the main thread to trigger an event to perform operations on the database, sometimes using a subroutine to perform data operations in the background. For example, data backup, transfer files and something. Other operations can be performed in the main window as well. And sometimes in the background each processing a data file, to the main window to send messages, so that the main window real-time display processing progress in the window (visible), while the log processing. I'm using the following method: [1] API function used: Registerwindowsmessage----------------------function Function: This function defines a new window message that ensures that it is unique within the system. The returned message value can be used when the function SendMessage or postmessage is called. functionRegisterWindowMessage (Lpstring:pchar): UINT;stdcall; SendNotifyMessage----------------------function Function: This function sends the specified message to a window.      If the window is created by the calling thread, this function calls the window program for the window and waits for the window program to finish processing the message before returning. If the window is created by a different thread, this function passes the message to the window program and returns immediately without waiting for the window program to finish processing the message. SendNotifyMessage (HWND hwnd,uint msg,wparam wparam,lparam iparam); Broadcastsystemmessage----------------------function Function: The function sends a message to the specified recipient. The recipient can be an application, an installation drive, a network drive, a system-level device drive, or a combination of these system components. [2] Process:typeTForm1=class(Tform) ....... ...... .........Privatemsg:cardinal; protected    procedureWndProc (varMessage:tmessage);Override;  Public        ...............        ...............  End; varForm1:tform1;  Msgstrlist:tstringlist; msgstrlock:tcriticalsection;ImplementationusesThreadcommunication_unit;{$R *.DFM}proceduretform1.formcreate (sender:tobject);beginMSG:= RegisterWindowMessage ('wm_threadmsg'); Msgstrlist:=tstringlist.create;End;procedureTform1.wndproc (varmessage:tmessage);begin  ifMessage.msg = MSG Then beginMsgstrlock.enter; ifMsgstrlist.count >0  Then beginCaption:= msgstrlist.strings[0]; Msgstrlist.delete (0); End;    Msgstrlock.leave; ShowMessage ('I got a message.'+IntToStr (message.msg)); End  Else begin    inherited; End;End;procedureTform1.button1click (sender:tobject);begintthreadcommunication.create (msg,memo1);End; ...............        ...............initializationMsgstrlock:=tcriticalsection.create;FinalizationMsgstrlock.free;End. A unit of a child-threading class:UnitThreadcommunication_unit;InterfaceusesClasses,stdctrls;typeTthreadcommunicaiton=class(TThread)Privatefmsg:cardinal;  Fmemo:tmemo; protected    procedureExecute;Override; proceduresendmsg;  Public    ConstructorCreate (Amsg:cardinal;am:tmemo);Virtual; End;Implementationusesmessages,windows, Dialogs,sysutils, threadmsg;{Tthreadcommunicaiton}Constructortthreadcommunicaiton.create (amsg:cardinal; am:tmemo);begin  inheritedCreate (True); Fmsg:=amsg; Fmemo:=am; Freeonterminate:=True; Resume;End;procedureTthreadcommunicaiton.execute;beginSynchronize (sendmsg);End;proceduretthreadcommunicaiton.sendmsg;varM:tmessage;  B:dword; D:integer;begin  {Place Thread code here}Sleep ( -); M.msg:=fmsg; B:=bsm_allcomponents;  Msgstrlock.enter; Msgstrlist.add ('Sub-line Cheng:'+inttostr (ThreadID) +'Send with Broadcastsystemmessage'); D:=Msgstrlist.count;  Msgstrlock.leave;  Broadcastsystemmessage (Bsf_postmessage, @B, m.msg, M.wparam, m.LParam); FMEMO.LINES.ADD ('Sub-line Cheng:'+inttostr (ThreadID) +'Send with Broadcastsystemmessage'+IntToStr (d));End;End. I put a memo control on the window to display some information. At the same time I define a global tstringlist variable that has some values to be passed out from the child thread. The message is sent with Broadcasesystemmessage, and the message number is passed in when the child thread is created. The message number is defined in formcreate with Registerwindowsmessage and a message number is obtained. The event handling after the message is triggered is written in WndProc. This writes the outgoing string of the child thread to the caption of the window. The tstringlist variable is used as a critical section because thread synchronization is required to prevent them from executing simultaneously when two threads access the global volume. Operate with the tcriticalsection. Enter, enter the critical section leave, and leave the critical section to correctly handle messages sent from the child thread. If the message is sent using the SendNotifyMessage function. Use the following: m.msg:=fmsg; SendNotifyMessage (Hwnd_broadcast,m.msg, M.wparam, m.LParam); If the parameter is Hwnd_broadcast, the message is sent to all the top-level windows in the system, Includes invalid or invisible non-owned windows, overridden windows, and pop-up windows, but messages are not sent to child windows. Because the message is sent to the main window with SendNotifyMessage, and the main window thread is the same as the calling thread, wait for the window program to finish processing the message before returning. To execute in a child thread: FMEMO.LINES.ADD ('Sub-line Cheng:'+inttostr (ThreadID) +'Send with SendNotifyMessage'); can also be usedfunctionPostMessage (Hwnd:hwnd; Msg:uint; Wparam:wparam; Lparam:lparam): BOOL;stdcallThe hwnd is the window handle, and you can send the message to the main window. Instead, SendNotifyMessage is sending the message to all the top-level windows. That is, if you have two instances running in your system. One of the messages sent in two instances will be received. and PostMessage because it is a message to the handle. Will only have a role in this instance of itself. 

Delphi7 sending messages from a child thread to the main thread triggering event execution

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.