Solved

CreateWindow and multiple threads

Posted on 1997-10-27
8
1,136 Views
Last Modified: 2013-12-03
Hello,

I am writing an application with an edit control in it. It seems that the normal way to do this (not using MFC but using the plain C-style SDK) is to create a main window and in the WndProc callback of the main window in the WM_CREATE event, create the edit control there. If I do that, everything works just fine. Now supposed I want to do something totally weird. That is, when the WM_CREATE event is reached, istead of calling CreateWindow to create the edit control there, I signal another thread to do the CreateWindow (passing it the appropriate information to do the create), and sleep() until the other thread respoonds that it is done, and then return from the WM_CREATE event. For some reason this does not work for me. The other thread always hangs (or in some infinite loop) in CreateWindow (inside USER32!), and I have no idea why.  

Is there something fundamental that I am missing ? For instance, win32 API forbids one to create child windows in a  thread different than the parent window ?  

   --sheila
0
Comment
Question by:scheng
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 15

Expert Comment

by:Tommy Hui
ID: 1407892
In most Win32 programs, the main thread is designated to work with UI elements. It has all the code that deals with creating and deleting windows. If a thread needs to have access to a particular window, then you can pass the window handle to the thread for it to use.

0
 
LVL 7

Expert Comment

by:faster
ID: 1407893
I suspect that it is because your new thread does not have a message loop.
0
 

Author Comment

by:scheng
ID: 1407894
I don't have a basic problem like not having the window handle. I have actually figured out my problem although the answer is, I can't do what I want to do. The "problem" is, while in WndProc, it will not process another message until either WndProc returns or WndProc calls another windows graphics function (ie. CreateWindow) that directly results in another message. I am in the latter case but since I moved the CreateWindow of the edit control into another thread with the original WndProc waiting (sleeping), it will not be preempted to process any messages that is a result of the CreateWindow from another thread.
0
SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

 
LVL 3

Accepted Solution

by:
vinniew earned 200 total points
ID: 1407895
you're right.
0
 

Author Comment

by:scheng
ID: 1407896
That's a good answer. :)  Can you answer this question. I'm pretty much playing with an edit control to write an editor that can edit unicode (wide character) text files that is better than Notepad. The edit control, I think, has a maxinum number of editable characters that you can put on it. I briefly saw something called a rich edit control that is supposed to be better. The question is, which is the best predefined text widget (control or window) to use for a general purpose editor that can handle an unlimited lines of text ?


0
 
LVL 11

Expert Comment

by:alexo
ID: 1407897
There is no problem creating controls in a different thread.

0
 

Author Comment

by:scheng
ID: 1407898
I know that there isn't a problem per se with creating a control in a different thread. But the way I was creating it would not work (according to Charles Petzold's Programming Windows 95). I was suspending the parent window's WndProc thread inside WndProc while trying to create a child control in another thread. When a child control is being created in another thread, the parent window's WndProc will get a message and it must be processed before the child control can be created (ie. before the create call returns). But since I have the parent's thread suspended, it isn't going to be able to process the message and hence I have my deadlock.

I believe that that is the difficulty with my original scheme of doing things. Please tell me if I'm wrong and why.


0
 
LVL 11

Expert Comment

by:alexo
ID: 1407899
exactly
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question