Application in postthreadmessage thread (taking multi-thread website data collection as an example)

Source: Internet
Author: User
Tags high cpu usage

Postthreadmessage refers to callback and sends messages to the thread. The following describes my application experience in the thread.
In Delphi, most of us use the tthread class that comes with the system to complete thread operations.
A class is generated from tthread and the Execute function is reloaded. It is defined as an abstract class (pure virtual Class C ++) in tthread ).
The subclass must reload this function. In this function, write the task code you want to complete. For example, the simplest application.
Tcustxxxxthread = Class (tthread)
Public
Procedure execute (); override;
End;

Tcustxxxxthread. Execute ()
Begin
While (not terminated) Do
Begin
// Do something
End;
// After exiting, the thread ends
End;

A thread needs to complete one or more tasks, which involves a basic and unavoidable problem. How to communicate with the thread and pass the input parameters to the thread
Obtain the running result of the thread. There are many implementation methods. Here we will focus on it.
 
In one of my software modules, we need to collect data from a website. Then, extract the required data and save it to the database.
To facilitate subsequent processing. The Indy control is used for network communication to download webpage text. Because the data volume is large, the website data is paged. I also want to loop here
Read the text of each webpage. Data download and decomposition are time-consuming operations. Naturally, they need to be put into the thread for running. To ensure that the main interface is not stuck.
Implemented pseudo code
While (ture) Do
Begin
(1) collect website text and notify the master thread that data has been downloaded.
(2) breaking down website text and extracting data,
While (ture) Do
Begin
Extract data records and send messages to the main thread (record N) for decomposition.
End;
If then exits from the last page of the collected website data.
End;

We often mention the system architecture when developing software. So what should the architecture of this module be.
1) all major processing functions run in the thread
2) notify the main thread of the Intermediate Running result for interface update.
3) network programs must consider error handling issues. For example, the website connection times out.
4) receive commands from the main thread to interrupt collection at any time.

Based on this situation and personal preferences. The main framework of the program is determined by simulating window message processing. Read messages,

Conversion and distribution are the main line.

The code framework is as follows:
Twebsample = Class (tthread)
Private
//..........
Protected
Procedure wndproc (VAR amsg: tmessage); // simulate window Message Processing
Procedure execute (); override; // main function run by the thread
Public
Mainwndhande: thandle; // Main Window handle, used for sending messages to the Main Window
End;

Twebsample. Execute ()
VaR
_ MSG: tagmsg;
Amessage: tmessage;
Begin
// Simulate message-driven,
// It serves as a message pump for receiving, analyzing, and forwarding messages.
While (not terminated) Do
Begin
If peekmessage (_ MSG, 0, wm_user, wm_user + 1100, pm_remove) then
Begin
Amessage. MSG: = _ msg. message;
Amessage. wparam: = _ msg. wparam;
Amessage. lparam: = _ msg. lparam;
Amessage. Result: = 1;
Wndproc (amessage); // message forwarding
If amessage. Result = 0 then
Break; // exit the thread
End
Else
Sleep (1); // avoid high CPU usage when no message comes in. The specific value can be determined by experiment.
End;
End;
 

Procedure twebsample. wndproc (VAR amsg: tmessage); // simulate window Message Processing
Begin
If amsg. MSG = msg_xxxx_01 then
Begin
// Message Processing
// Send a message to the main form for data update and other operations
Sendmesesage (mainwndhand, msg_main_xxxx, wparam, lparam );
End
Else
If amsg. MSG = msg_xxx_02 then
Begin
// Message Processing
End;
End;

// After thread introduction, let's talk about how the main form sends messages (commands) to the pseudocode of the thread.
Procedure tfrmsaleanaly. btngoclick (Sender: tobject );
VaR
Threadobj: twebsamplebj;
Begin
Threadobj: = twebsamplebj. Create (true );

Thredobj object initialization
Thredobj. Resume (); // start the thread to run

// Issue the control command to the thread
Postthreadmessage (threadobj. threadid,
Msg_xxxx_01,
Wparam,
Lparam );
 
End;

If no response is returned when the message is delivered to the thread, try the following method:
Procedure postthreadmessageeh (threadid: DWORD; MSG: DWORD; wparam, lparam: integer );
Begin
While (not postthreadmessage (threadid, MSG, wparam, lparam) do sleep (5 );
End;

Msdn's solution to this problem.

The thread to which the message is posted must have created a message queue, or else the callPostthreadmessageFails. Use one of the following methods to handle this situation:

  • CallPostthreadmessage. If it fails, call
    SleepFunction and callPostthreadmessageAgain. RepeatPostthreadmessageSucceeds.

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.