Create a window in a non-main thread

Source: Internet
Author: User

Many of my friends have had this experience. Why do I create a window in the main thread and the window works normally? But when I move it to a non-main thread (some friends like to call it a working thread ), but cannot work normally. this article will discuss this issue with you and may not be able to do its utmost, but it can be a relief.

Create a window in the main thread that can work normally.

This is a normal operation.Code:

# Include "  Windows. h  "  Hwnd g_hwnd = NULL; hinstance g_hinst; lresult wndproc (hwnd, uint wmsg, wparam, lparam ){  Return  Defwindowproc (hwnd, wmsg, wparam, lparam );}  Void Createwnd (Void  ) {Wndclass WC = { 0  }; WC. Style = 0  ; WC. lpfnwndproc = Wndproc; WC. cbclsextra = 0  ; WC. cbwndextra = 0  ; WC. hinstance = G_hinst; WC. hicon = NULL; WC. hcursor = Loadcursor (null, idc_arrow); WC. hbrbackground = (Hbrush) getsyscolorbrush (color_window); WC. lpszmenuname = NULL; WC. lpszclassname = Text ( "  Simplewindow  "  ); Registerclass ( & WC); g_hwnd = Createmediawex ( 0  , Text (  "  Simplewindow "  ), Text (  "  Simplewindow  "  ), Ws_visible,  0  ,  0  ,  200  ,  200  , Null, null, g_hinst,  0 );}  Int  Winapi winmain (hinstance, hinstance hprevinstance, lptstr lpcmdline,  Int  Ncmdshow ){  //  Todo: Place code here.  G_hinst = Hinstance; createwnd ();  //  The message loop  MSG;  While (Getmessage (& MSG, null, 0 , 0  ) {Translatemessage ( & MSG); dispatchmessage ( & MSG );}  Return   0  ;} 

If we create a thread and create a window in the thread, let's see what it brings to us:

# Include "  Windows. h  "  Hwnd g_hwnd =NULL; hinstance g_hinst; lresult wndproc (hwnd, uint wmsg, wparam, lparam ){  Return  Defwindowproc (hwnd, wmsg, wparam, lparam );}  Void Createwnd ( Void  ) {Wndclass WC = { 0  }; WC. Style = 0  ; WC. lpfnwndproc = Wndproc; WC. cbclsextra = 0 ; WC. cbwndextra = 0  ; WC. hinstance = G_hinst; WC. hicon = NULL; WC. hcursor = Loadcursor (null, idc_arrow); WC. hbrbackground = (Hbrush) getsyscolorbrush (color_window); WC. lpszmenuname = NULL; WC. lpszclassname = Text ( "  Simplewindow  " ); Registerclass ( & WC); g_hwnd = Createmediawex ( 0  , Text (  "  Simplewindow  "  ), Text (  "  Simplewindow  "  ), Ws_visible,  0  ,  0 ,  200  ,  200  , Null, null, g_hinst,  0  );} DWORD createthread (pvoid parg) {createwnd ();  Return   0  ;}  Int  Winapi winmain (hinstance, hinstance hprevinstance, lptstr lpcmdline, Int  Ncmdshow ){  //  Todo: Place code here.  G_hinst = Hinstance; handle hthrd = Createthread (null, 0 , Createthread, null, 0  , Null); closehandle (hthrd );  //  The message loop  MSG;  While (Getmessage (& MSG, null,0 , 0  ) {Translatemessage ( & MSG); dispatchmessage ( & MSG );}  Return   0  ;} 

We didn't seem to have seen anything, but the window was just a flash, and everything was gone. because g_hwnd is a global variable, it tells us that g_hwnd will not be destroyed before the main thread exits. with breakpoint debugging, you will find that in the wndproc function, you can only receive wm_create and some subsequent messages, and the subsequent messages will no longer be received, especially the wm_paint seems to disappear out of thin air! So, the code hasn't changed anything, just moved to the sub-thread. Why is this problem?

Everything seemed simple, and we found the answer in msdn (see: http://support.microsoft.com/kb/90975/en-us ):

In a multithreaded application, any thread can call the createwindow () API to create a window. There are no restrictions on which thread (s) can create windows.

It is important to note that the message loop and window procedure for the window must be in the thread that created the window. if a different thread creates the window, the window won't get messages from dispatchmessage (), but will get messages from other sources. therefore, the window will appear but won't show activation or repaint, cannot be moved, won't receive mouse messages, and so on.

In this section, the window can be created in any thread, but the message loop must be in the same thread as the creation window. Otherwise, the window cannot get any message from dispatchmessage!

It is important to note that the message loop and window procedure for the window must be in the thread that created the window.

Well, let's place the message loop code in the route to see what the result is:

# Include "  Windows. h  "  Hwnd g_hwnd = NULL; hinstance g_hinst; lresult wndproc (hwnd, uint wmsg, wparam, lparam ){  Return  Defwindowproc (hwnd, wmsg, wparam, lparam );}  Void Createwnd ( Void  ) {Wndclass WC = { 0  }; WC. Style = 0  ; WC. lpfnwndproc = Wndproc; WC. cbclsextra = 0  ; WC. cbwndextra = 0  ; WC. hinstance = G_hinst; WC. hicon =NULL; WC. hcursor = Loadcursor (null, idc_arrow); WC. hbrbackground = (Hbrush) getsyscolorbrush (color_window); WC. lpszmenuname = NULL; WC. lpszclassname = Text ( "  Simplewindow  "  ); Registerclass ( & WC); g_hwnd = Createmediawex ( 0  , Text (  " Simplewindow  "  ), Text (  "  Simplewindow  "  ), Ws_visible,  0  ,  0  ,  200  ,  200 , Null, null, g_hinst,  0  );} DWORD createthread (pvoid parg) {createwnd ();  //  The message loop  MSG;  While (Getmessage (& MSG, null, 0 , 0  ) {Translatemessage ( & MSG); dispatchmessage ( & MSG );} Return   0  ;}  Int  Winapi winmain (hinstance, hinstance hprevinstance, lptstr lpcmdline,  Int  Ncmdshow ){  //  Todo: Place code here.  G_hinst = Hinstance; handle hthrd = Createthread (null, 0 , Createthread, null, 0 , Null); closehandle (hthrd); MSG;  While (Getmessage (& MSG, null, 0 , 0  ) {Translatemessage ( & MSG); dispatchmessage ( & MSG );}  Return   0  ;} 

Everything is normal, just like creating it in the main thread!

Of course, note that in this example, message loops exist in both the main thread and thread. If postquitmessage () is called in wndproc (), the main thread will keep waiting for messages, resulting inProgramUnable to exit normally. however, you don't need to worry too much. Unlike the sample code, in actual code writing, the main window is often created in the main thread, and the message processing function in the main window calls postquitmessage () the main thread can exit normally.

The fact tells us that the window created by a non-main thread can also work normally, as long as we note that the message loop must be in the same thread as the window created!

Address: http://blog.csdn.net/norains/article/details/2023957

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.