Solved

CreateWindow and multiple threads

Posted on 1997-10-27
8
1,096 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
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
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.

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying 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

Suggested Solutions

Title # Comments Views Activity
Visual Fox Pro commands 15 50
Windows 10 Scheduled Tasks 11 98
What are new names for Microsoft test frameworks once called Moles and Pex? 2 61
Need more details 5 168
This article shows a few slightly more advanced techniques for Windows 7 gadget programming, including how to save and restore user settings for your gadget and how to populate the "details" panel that is displayed in the Windows 7 gadget gallery.  …
For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
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…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

856 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