Click here to Skip to main content
14,933,981 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I post user defined create window message from the main thread,and call the CreateWindow() function ,but it does't return NULL or valid HWND,just stay still there.
Any guys have faced this issue?
Thanks.
Below is part of the code:
std::cout << " begin create window..... " << std::endl;
                                globals->new_window = CreateWindowEx (0,globals->window_class_name,  TEXT(""),
                                                                    globals->dwStyle,
                                                                    CW_USEDEFAULT, CW_USEDEFAULT,
                                                                    CW_USEDEFAULT, CW_USEDEFAULT,
                                                                    NULL, NULL, globals->hInstance, NULL);
std::cout << "end create window ... " << std::endl;


I post the msg in main thread:
globals->dwStyle = dwStyle_;
if (PostMessage(globals->helper_window,WM_USER+CREATE_WINDOW,0,0)==0)
{
    throw gui_error("Unable to schedule function for execution in event handling thread.");
}


It print the
begin create window.....
,but not go down.

What I have tried:

I have checked all the code ,but not work.
Posted
Updated 19-Jul-18 20:41pm
v3
Comments
OriginalGriff 20-Jul-18 2:10am
   
Show us the relevant code fragments ...

Use the "Improve question" widget to edit your question and provide better information.
Jochen Arndt 20-Jul-18 3:04am
   
It is still not possible to understand what is happening. We don't know what all these 'global' members are and what they contain, and when and how the posted code is called. It seems also that different threads are involved.

The usage of 'global' indicates that you have at least a bad design. If this is used from different threads it will very probable not work without some kind of locking mechanism.

Note also that you should use WM_APP instead of WM_USER when posting messages between different windows.
Member 13919032 20-Jul-18 3:28am
   
Dear Jochen,Thank for your reply.
"global" store some vars used between different threads.
PostMessage is successful ,because it has run to CreateWindow() of WndPro in another thread,but just not return from it.
May i know the diffrence between WM_APP and WM_USER?
Jochen Arndt 20-Jul-18 3:45am
   
When using variables shared by threads, you must ensure that only one thread at time accesses these. I don't see any method doing that in your code.

PostMessage() is the method to be used when passing messages from one thread to another. But I don't know about the organisation of your application (what is called when by which thread) and what values these 'globals' actually hold.

I still guess that your problem is sourced by a bad thread design. Maybe your design is even wrong and can't work as intended.

A general approach is executing all GUI operations (including window creation) in the main (GUI) thread and doing background operations in worker threads which use PostMessage() to send data or notifications to GUI elements (windows or controls). For the other direction use events for signaling. For shared data use appropriate locking mechanisms.

See the MS documentation of WM_APP / WM_USER. It is explained in detail there.
Richard MacCutchan 20-Jul-18 3:47am
   
It sounds like that thread is getting blocked by something else in your code.
Member 13919032 20-Jul-18 4:59am
   
Maybe this is the problem,I'm checking it. Thank you,Richard.
Member 13919032 20-Jul-18 5:07am
   
Dear Richard, could you help me to explain why CreateWindow() in WndProc directly send Msg (WM_CREATE) and go into WndProc again without out the WndProc?
Thanks.
Richard MacCutchan 20-Jul-18 7:32am
   
Because that is the way that CreateWindow operates, as explained in the documentation. If you do not capture WM_CREATE then the default will do nothing, and just return a positive response. I suspect there is some other code that you have not shown us that is the cause of your problem.
11917640 Member 22-Jul-18 5:55am
   
The reason is probably in new window WndProc function, registered with globals->window_class_name.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900