Delphi7 sends messages from the Child thread to the main thread to trigger event execution

Source: Internet
Author: User
Tags sendmsg

During database operations, a sub-thread is sometimes used for background data operations. For example, data backup, transfer, or something. Other operations can be performed in the main window. Sometimes, each time a data file is processed in the background, a message must be sent to the main window so that the processing progress of the main window can be displayed in real time on the window (visualized), and logs can be processed at the same time. I use the following method:

[1] API functions used:
Registerwindowsmessage
----------------------
Function: This function defines a new window message, which must be unique in the system. The returned message value can be used when the sendmessage or postmessage function is called.
Function registerwindowmessage (lpstring: pchar): uint; stdcall;

Sendpolicymessage
----------------------
Function: This function sends a specified message to a window.
If the window is created by the calling thread, this function calls the window program for the window,
Wait until the window program finishes processing the message and then return.
If the window is created by different threads, this function sends messages to the window program,
Return immediately without waiting for the window program to finish processing the message.
Sendpolicymessage (hwnd, uint MSG, wparam, lparam iparam );

Broadcastsystemmessage
----------------------
Function: This function sends a message to a specified receiver.
Receiver can be an application, installation drive, network drive, system-level device drive
Or a combination of these system components.

[2] process:
Type
Tform1 = Class (tform)
...............
...............
Private
MSG: Cardinal;
Protected
Procedure wndproc (VAR message: tmessage); override;
Public
...............
...............
End;

VaR
Form1: tform1;
Msgstrlist: tstringlist;
Msgstrlock: tcriticalsection;

Implementation
Uses threadcommunication_unit;
{$ R *. DFM}

Procedure tform1.formcreate (Sender: tobject );
Begin
MSG: = registerwindowmessage ('wm _ threadmsg ');
Msgstrlist: = tstringlist. Create;
End;

Procedure tform1.wndproc (VAR message: tmessage );
Begin
If message. MSG = MSG then begin
Msgstrlock. enter;
If msgstrlist. Count> 0 then begin
Caption: = msgstrlist. Strings [0];
Msgstrlist. Delete (0 );
End;
Msgstrlock. leave;
Showmessage ('Received message' + inttostr (message. msg ));
End
Else begin
Inherited;
End;
End;

Procedure tform1.button1click (Sender: tobject );
Begin
Tthreadcommunication. Create (MSG, memo1 );
End;
...............
...............

Initialization
Msgstrlock: = tcriticalsection. Create;
Finalization
Msgstrlock. Free;
End.

The unit of a sub-Thread class:
Unit threadcommunication_unit;
Interface

Uses
Classes, stdctrls;

Type
Tthreadcommunicaiton = Class (tthread)
Private
Fmsg: Cardinal;
Fmemo: tmemo;
Protected
Procedure execute; override;
Procedure sendmsg;
Public
Constructor create (amsg: Cardinal; am: tmemo); Virtual;
End;

Implementation
Uses messages, windows, dialogs, sysutils, threadmsg;

{Tthreadcommunicaiton}

Constructor tthreadcommunicaiton. Create (amsg: Cardinal; am: tmemo );
Begin
Inherited create (true );
Fmsg: = amsg;
Fmemo: = am;
Freeonterminate: = true;
Resume;
End;

Procedure tthreadcommunicaiton. Execute;
Begin
Synchronize (sendmsg );
End;

Procedure tthreadcommunicaiton. sendmsg;
VaR
M: tmessage;
B: DWORD;
D: integer;
Begin
{Place thread code here}
Sleep (50 );
M. MSG: = fmsg;
B: = bsm_allcomponents;

Msgstrlock. enter;
Msgstrlist. Add ('child thread subhandle: '+ inttostr (threadid) +' send with broadcastsystemmessage ');
D: = msgstrlist. count;
Msgstrlock. leave;

Broadcastsystemmessage (bsf_postmessage, @ B, M. MSG, M. wparam, M. lparam );
Fmemo. lines. Add ('child thread subhandle: '+ inttostr (threadid) +' send with broadcastsystemmessage' + inttostr (d ));

End;

End.

I have a memo control in the window to display some information.
At the same time, I defined a global tstringlist variable for some values to be passed out from the subthread. The broadcasesystemmessage is used to send messages, and the message number is passed in when the subthread is created. The message number is defined by registerwindowsmessage in formcreate and a message number is obtained.
The event processing after message triggering is written in wndproc.
Write the string passed out from the sub-thread to the title of the window.

Tstringlist variables are used as the critical section, because when two threads access the global volume, to prevent them from being executed simultaneously, thread synchronization is required.

Use tcriticalsection.
Enter to enter the critical section
Leave, exit the critical section
In this way, messages sent from sub-threads can be correctly processed.

If the sendpolicymessage function is used to send messages.
The usage is as follows:
M. MSG: = fmsg;
Sendpolicymessage (hwnd_broadcast, M. MSG, M. wparam, M. lparam );

If the parameter is hwnd_broadcast, the message will be sent to all top-level windows in the system, including invalid or invisible non-self-owned windows, overwritten windows, and pop-up windows, however, messages are not sent to subwindows.

Because sendpolicymessage is used to send messages to the main window, And the thread where the main window is located is the same as the calling thread, you have to wait until the window program finishes processing the messages and then return them. In the subthread:

Fmemo. lines. Add ('child thread subhandle: '+ inttostr (threadid) +' send with sendpolicymessage ');

You can also use
Function postmessage (hwnd: hwnd; MSG: uint; wparam: wparam; lparam: lparam): bool; stdcall;
Hwnd is the window handle. You can send messages to the main window.
Sendpolicymessage sends messages to all top-level windows. That is to say, if you start two instances in the system to run.
Messages sent from one instance are received by both instances. Postmessage sends messages to the handle. It only works in this instance.

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.