Click to See Complete Forum and Search --> : _beginthreadex vs CreateWindowEx


**TheStig**
July 16th, 2007, 08:09 PM
This is probably a question a newbie worthy.. Which seems appropriate..

I have created a thread by using _beginthreadex. This new thread (lets call it mother) has a callback function which seems to work just fine. However, this thread (mother) requires a window to show and handle some UI. The thread creates a window (lets call it child) using CreateWindowEx which has its own callback function.

The problem is that the window (child) is not responding to anything. Simple moving around/resizing is not happening. I realize that using two threads "on top of" each other for showing a simple window seems complicated, which it is. I am trying to simplify the code structure all the time.

Back to the core problem. Why is the window (child) not responding? I have put in trace events to verify that the window (child) callback is really being called, which it seems to be. The problem is when I try to send messages from "mother" by using the HWND (as obtained from the call to _beginthreadex), the message seems stuck in the mother thread. It is captured in mother, not in the child callback. Well, not instantly at least. It comes through after a while (but mother could have had several hundred hits already). The CPU is struggling, and I have to force the app to a shutdown/stop.

Message handling is done in Mother's callback. I have a timer event which spins through PeekMessage and calls TranslateMessage/DispatchMessage.

This entire problem could be based on poor design, but it should work, should'nt it?

Any ideas? Hopeless case?

Arjay
July 16th, 2007, 08:59 PM
Generally you would put all the UI components in the same thread. That said, do you have a message pump in the child thread?

**TheStig**
July 18th, 2007, 05:07 PM
Thanks for your reply.

I have tested with message pumps, yes. Various versions actually (with the two-thread solution in mind), but none seems to help. I have made a slight redesign though.

I have removed the Mother. Now I create the form thread only (using CreateWindowEx), being left with the child only. The problem now is to create a messagepump which does not halt the continued program execution (I have other threads which are supposed to be created later for instance). CreateWindowEx sends three or four messages to my form callback. These are trapped correctly. The problem is that CreateWindowEx uses what seems to be SendMessage, which again results in me not being able to use one of the received messages to start a message pump (while(true)). This would halt further program execution since SendMessage never returns.

_beginthreadex handles this very easily by use of the argument list.

Any suggestions?

Arjay
July 18th, 2007, 06:11 PM
I am confused with the architecture.

Here is what I would do:

1) Create all the UI in one thread.
2) Create worker thread(s)
3) Define WM_USER messages to for the worker thread(s) to notify changes in data
4) Protected any resources shared between the UI thread and worker thread(s) with a critical section.
5) When the worker thread gets data it locks the shared resource, updates it then calls PostMessage to let the UI thread know the data has been updated, then unlocks the shared resource.
6) The main UI creates WM_USER message handlers to respond to the worker thread data changes. When receives a Wm_User message, it locks the appropriate shared resource, updates the UI, and then unlocks the resource.

**TheStig**
July 18th, 2007, 06:34 PM
I understand your confusion... I have been using days to think of this.

How do you create the worker threads? Message only windows? Or some other trick using _beginthreadex?

JamesSchumacher
July 21st, 2007, 06:13 PM
CreateWindowEx does not create a thread, it creates a window. Without a message pump in the thread that it resides in, then the window will not respond to messages. You have to use GetMessage, PeekMessage, TranslateMessage, DispatchMessage....

CreateThread creates a thread, and so does _beginthreadex.