Windows message loop

Source: Internet
Author: User
Today, I am working with my colleagues to call a piece of code, which has been depressing for a long time.

The calling process is not complicated. It is just that he provides a DLL and outputs a function (also called foo (). I call its function Foo to generate a window internally. However, every time I call a window, the window will always flash! We suspect that the main program is the console and does not support MFC? Otherwise, it must be because it is not called in the main thread ?! It's a lot of trouble!

The results are as follows:

MFC dialog box Win32 MFC Console
Multi-thread failure
Single-thread success failed

Why? Can't I create a window in a normal thread? What is a main thread (ui thread) and a working thread? What I used to take for granted suddenly becomes blurred!

Start to lock your eyes on the message queue! Is there a message queue in the main thread? For example, the MFC Dialog Box program has a message queue, but the Console does not. However, it's also a thread. Why do you have it and I don't have it? Is the winmain function different from the normal main function? When is the message queue generated?

Caicao browsed the cwinapp code and did not find the answer. At this time, I vaguely remembered what Windows core programming said. The system automatically creates a message queue for the thread when you call related functions for the first time! But what is a function?

Back home with this depressing, open the windows programming book and go to the Message Queue chapter. Originally, each window belongs to the thread that created it. Once the thread exits, the window will be destroyed on its own! In addition, the thread needs to cycle messages to distribute messages in the window! Note that the message loop is the responsibility of the thread! When a thread is created at the beginning, it does not contain a message queue. When a thread calls window-related functions for the first time, the operating system automatically creates a message queue for it! Originally, the message queue of the window belongs to the thread!

The problem is finally clarified. In the normal working thread, there is no message loop, so the created window has no chance to be displayed, and the thread stops and exits! Even if the program waits through cin. Get () in the console program, the window still does not have the opportunity to refresh (dependent on message loop distribution and calling the message processing function) and remains in the busy waiting state! The previous Foo function depends on the message loop!

After knowing the real cause of the problem, it is easy to solve it! After calling a colleague's function to generate a window (the function returns), the message loop is performed in the same thread! As follows:

... In a working thread...

Foo (); // call some function

While (getmessage (& MSG, null, 0, 0 ))
{
If (! Translateaccelerator (msg. hwnd, hacceltable, & MSG ))
{
Translatemessage (& MSG );
Dispatchmessage (& MSG );
}
}

... Working thread exit...

Solve the problem! You don't think it's an issue with MFC, or you cannot create a window in the working thread. Originally, I suspected from the very beginning that such dependency on MFC never allowed the main program to be an MFC program! As a DLL component, there cannot be too many restrictions on callers! Multithreading is too common. If it is all done in the main thread, it will be too difficult to modularize it! Of course, now I am talking about all this. When I was depressed, I used all the weird reasons!

In fact, this problem can be solved more quickly. If a colleague explains in advance when his Foo function will return (I always thought it would be blocked until the generated window is closed, but the result is not like this, when a window is generated, it is returned.) and whether it depends on the external message loop (he mentioned it, but it is also vague, so he cannot figure out the specific cause ). Of course, the biggest reason is that you are not very familiar with the message loop mechanism of windows!

Now, do you understand how message loops in windows are implemented?

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.