NO MFC Programming 05 - Process> Thread> Message Queue, Triple Contains Relationship

zhaozj2021-02-16  40

(The following content is impossible for people who started to get started)

Process and threads, threads, and messages, there is indeed a relationship between them. If I let me sort by size, I will arrange the topic. (However, someone will say how can the message queue will talk to the process, threads are talking about it?)

Simply, what is a process? What is thread? For an alteration, your program is to be executed, the operating system will load your EXE file into the memory, then generate a process (of course also contains allocated resources, etc.); For threads, you can understand it is one Different parts in the program, this is a similar function, and the difference is that each thread is executed simultaneously. For example, your primary thread creates another secondary thread, then the two threads are working at the same time, there is no call - the concept of returning. There can be multiple threads in one process in execution, called an executive example.

Based on my understanding, the process should be a big concept, at least one main thread (ie, the main execution instance) at the beginning, which is the main execution process created when the system is loaded. Generally I can only see the process for the outside, for example, in the Win2000's task manager, only the process (Process) is seen.

Use Ctrl Shift ESC to adjust the task manager in Win2000.

The message queue is related to thread, in other words, one thread can only have a message queue (Queue) corresponding to it. This is a bit different from before, and there can be multiple threads in a process; but there is no message queue (Win98 can even have no message queue).

When is the message queue generated? Inside Win2000, it is already created from the beginning. (In WIN98, I estimate that after you create a window, leave it to you)

Half a day, maybe some friends who have just started don't know what is the message queue? In fact, the Windows operating system is an event-driven system. It grasped the message queue such as a mouse, keyboard input and other things, and send it to your program. Your program takes an event each time, performs the corresponding operation according to the nature of the event, constantly looping.

Microsoft advocates programming methods for programmakers using event-driven programming methods. You can also send fake news in the messages of our thread, and you can also deceive yourself or you can (hypocritical)! Use the PostThreadMessage function.

The program that compares multithreading is not difficult, and the difficulty is actually in thread synchronization (thread coordination work), the following source program is intended to introduce multithreaded programming. (Don't forget the entrance of the main thread is Winmain function)

// file name: WinMain.cpp

#define Win32_Lean_and_mean // Say No To Mfc !!

#include

CHAR TEMP [77] = ""

// I have custom new thread entry mythread function DWORD WINAPI Mythread (lpvoid lpparameter) {long thrval = 1;

For (thrval = 1; thrval <16; thrval ) {wsprintf (Temp, "This is the secondary line% LD display", thrval); MessageBox (NULL, TEMP, "Second thread content __copyright -` Sea Breeze ", MB_OK | MB_TOPMOST;} Return 1;

} // End of the secondary thread

// name: WinMain () main thread entrance // ------------------------------- INT WinApi Winmain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NCMDSHOW) {

DWORD DWTHREADID, DWMYTHRDPARAM = 1; // The first parameter is the ID number of the new thread, the second parameter slightly Handle Hthread; // Subverse Handle

// Call function Create a new thread, the new thread entry is the Mythread function hthread = Createthread (null, // no (or default) attribute 0, // Using the default stack size mythread, // My thread portal function & dwmythrdparam, / / argument to thread function 0, // Using the default Creation Flags & DWTHREADID); // Win98 can not be omitted

// has created a new secondary thread

MessageBox (NULL, "has created a new secondary thread, / n" "" Press OK to end the main thread! "," Main thread information display __copyright - `sea breeze", MB_OK | MB_TOPMOST);

EXITPROCESS (0); return null;}

If there is only one message queue (in a thread), how many messages can be accommodated? I have programs to verify:

// file name: WinMain.cpp

#define Win32_Lean_and_mean // Say No To Mfc !!

#include

CHAR TEMP [77] = "Hello World";

// name: WinMain () // ------ --------------------------- Int Winapi WinMain (Hinstance Hinstance, Hinstance Hprevinstance, LPSTR LPCMDLINE, INT NCMDSHOW) {MessageBox (NULL, "Simple_code __copyright -` Sea Breeze ", MB_OK | MB_TOPMOST);

// The above sentence, please use Win98 friends to delete, because after the function called created the window, your thread has a message queue!

DWORD CURTHREADID = GetCurrentThreadId (); // Get the ID (identification number) of the current thread

Long i = 1; for (i = 1; i <900000; i ) {if (! PostthreadID, WM_USER, 11, 22)) BREAK; / / The above sentence is if it is no longer adding a message, interrupt the for loop } WSPRINTF (Temp, "Specific Message Queue Length is% LD", i);

MessageBox (Null, Temp, "Sample_code __copyright -` Sea Breeze ", MB_ok | MB_TOPMOST

EXITPROCESS (0); return null;}

The result of the display is: 10000, is there so big? ! I also don't believe it.

If there is no use of 10000, what will happen? Oh, of course it is lost!

In order to prove that the message queue is an accessory of threads, I viewed MSDN, and finally I found evidence in the first parameter of the postthreadMessage () function. The original text is as follows:

"The function fails if the specified thread does not have a message queue. The system creates a thread's message queue when the thread makes its first call to one of the Win32 User or GDI functions. For more information, see the Remarks section."

The meaning is probably that when a Win32 User class function (or graphical interface function) is called in a thread, the system creates a message queue for the thread, otherwise there is no message queue.

However, this statement I think it is not applicable in Win2000. In fact, a related message queue (default operation) is created while the system creates thread.

I have tried to send a message to the queue without calling any other functions.

This queue maintained by the system is also very automated! To make a simple example is that the system automatically adjusts the number of WM_PAINT messages will not be repeated; more complex examples send messages to one window in the thread, then the destroy window, finally detect the message queue. The system will think that the window has no excess message to delete the excess message related to the window, so you must not find the message you sent before you send it in the message queue. Ok, say too much, I am afraid of chaos your ideas, please refer to MSDN, please enter "Message Queues [win32] in the MSDN index."

I have time to have time, I will explain the message pump, this play! `Sea Breeze October 12, 2002 PM 2:28

-------------------------- Temp77 origin:

Life is in a hurry, but it is quite a temporary variable because I was born in 77 years, so I named TEMP77!

Currently like songs: Kuba Tianli - Never Turn Back

转载请注明原文地址:https://www.9cbs.com/read-24605.html

New Post(0)