Use Win32 API in Delphi to create built-in thread functions for message Loops

Source: Internet
Author: User

Use Win32 API in Delphi to create built-in thread functions for message Loops

1. Thread Creation
Handle createthread (
Lpsecurity_attributes lpthreadattributes,
// Pointer to thread security attributes
DWORD dwstacksize, // initial thread stack size, in bytes
Lpthread_start_routine lpstartaddress, // pointer to thread function
Lpvoid lpparameter, // argument for new thread
DWORD dwcreationflags, // creation flags
Lpdword lpthreadid // pointer to returned thread identifier

);

Example:
VaR
Hmythread: thandle; // thread handle
Ithreadid: DWORD = 0; // thread ID

Begin
Hmythread: = createthread (nil, 0, @ threadfun, nil, 0, ithreadid );
..
End;

Among them, threadfun is the thread function or process you define. Note that because the Win32 API is used to create a thread, this threadfun function must be set to stdcall;
Example: Procedure threadfun; stdcall;

2. Send a message to the subthread

Bool postthreadmessage (

DWORD idthread, // thread identifier
Uint MSG, // message to post
Wparam, // first Message Parameter
Lparam // second Message Parameter
);

Example:
Const
Wm_mymsg = wm_user + 100; // custom message

Postthreadmessage (ithreadid, wm_mymsg, 0, 0 );

The preceding statement shows that the main thread sends the wm_mymsg message to the ithreadid subthread. However, when sending messages to the subthread 1st times, it should be sent cyclically until the message is sent successfully. In this way:

While not postthreadmessage (ithreadid, wm_mymsg, 0, 0) Do
Sleep (100 );

3. Destroy sub-threads
1. postthreadmessage (ithreadid, wm_quit, 0, 0 );

2. closehandle (hmythread );
Bool closehandle (
Handle hobject // handle to object to close
);

If the ithreadid subthread has a built-in message loop, when the getmessage in the thread receives the wm_quit message, the ithreadid subthread will be destroyed.

4. The subthread has a built-in message loop.
The following APIs are required:
A. Get the message: getmessage () or peekmessage ()

Bool getmessage (
Lpmsg, // address of Structure with message
Hwnd, // handle of window
Uint wmsgfiltermin, // first message
Uint wmsgfiltermax // last message
);

Bool peekmessage (
Lpmsg, // pointer to structure for message
Hwnd, // handle to window
Uint wmsgfiltermin, // first message
Uint wmsgfiltermax, // last message
Uint wremovemsg // removal flags
);

The two message retrieval functions are a little different.
When the getmessage () function retrieves a message from the thread message queue, if there is no message in the message queue at this time, the thread will be frozen, it is not activated until another thread sends a message to the thread.
When the peekmessage () function retrieves a message from a thread message queue, if there is no message in the message queue at this time, the operating system still allows the thread to execute for a period of time. In other words, it only serves to check whether there are messages in the thread's message queue. In my opinion, this function is more useful than the getmessage () function. Of course, the getmessage () function is also useful in some cases.

B. Translate and dispatch messages.
Translatemessage (MSG); // translate the message
Dispatchmessage (MSG); // dispatch a message

Bool translatemessage (
Const MSG * lpmsg // address of Structure with message
);

Long dispatchmessage (
Const MSG * lpmsg // pointer to structure with message
);

The above two functions are the core of the message loop. They are used together with getmessage () or peekmessage () to construct the message loop-the heart of the program

Example:
VaR
MSG: tmsg;
Fquit: Boolean;

Begin
While not fquit do
Begin
While peekmessage (MSG, 0, 0, 0, pm_remove) Do
// While getmessage (MSG, 0, 0, 0) Do
Begin
If MSG. Message = wm_quit then
Begin
Fquit: = true;
End
Else
If MSG. Message = wm_mymsg then
Begin
..
End
Else
Begin
Translatemessage (MSG );
Dispatchmessage (MSG );
End;
End;
End;

End;

In the above example, peekmessage () and getmessage () are put together to save time. Of course, you need to understand that if getmessage () is used, if MSG. Message = wm_quit then is useless, it only exists for the above peekmessage. Because when the thread getmessage () receives wm_quit, The getmessage () loop exits.

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.