MDI with a User Interface Thread

Posted on 1998-09-16
Last Modified: 2013-11-20
I have a MDI project with several Documents and Views.
I would like to add to the project a CwinThread class (User Interface Thread).
I want this class to have its own message queue (since it different thread),
But the GUI's thread frame should be and behave like regular CMDIChildwns.
How it can be done?
Question by:yoavm
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

Expert Comment

ID: 1322182
I think there is no way to do that. If you want to achieve that from your separate user interface thread you have to send WM_MDICREATE to the MDI client window, which, however, is created by the main thread and thus it just does not work, i.e. the MDI client window is created in the main thread and cannot produce new MDI windows to another thread. This simply blocks your way to do what you want. But there sure are other solutions to your problem. Can you specify why you need the separate thread? Then maybe some different solution will pop up here :-)

Expert Comment

ID: 1322183
You can get around the problem of sending WM_MDICREATE to the MDI client window for creation of the new MDI child window, by calling CreateMDIWindow in the child UI thread. I order to do this, you need to completly write your own code to create the frame and view.

The above is no problem, but here comes the trouble: MFC maintains a thread local map of HWND -> CWnd. If you create a new frame and view in another thread than the main, MFC will ASSERT a lot of places because HWND maps to the wrong CWnd object (a temp one).

I have found a solution to this, and it seems to work ok, but I need to tweek the creation of the child frame a little bit more before posting the solution here.

Author Comment

ID: 1322184
to the  piano_boxer:

I would like very much to get this solution.

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Expert Comment

ID: 1322185
Nearly done!
Works great now, I just need to insert code to correctly shutdown the child UI thread(s) when closing the mainframe, and make the UI child thread init function support a user supplied CMultiDocTemplate instead of the hardcoded values i use for testing.

Expert Comment

ID: 1322186
At this point I have to say that there is really no reason for creating each view in its own thread. Executing blocking function (Fx: Sleep(10000)), will block the whole MDI user interface anyway. (I suspected that).

I tried to create a toolbar in the context of the child thread, and press a button on it to start executing the blocking function. The function is executed correctly in the context of the child thread BUT still blocks the UI :-(
Leave your e-mail here and I'll send the source-code to you (Its really to long to post here).

Expert Comment

ID: 1322187
Can I have the source as well;

Author Comment

ID: 1322188

My email:


Author Comment

ID: 1322189

My email:

LVL 11

Accepted Solution

mikeblas earned 1000 total points
ID: 1322190
The architecture you suggest is a waste of time. MDI windows are child windows, and will necessarily communicate with their parent window using sent messages. Those sent messages force context switches between the sending thread and the receiving thread, with the sending thread blocking until the receiving thread handles the message.

That's inefficient--you won't get the full multithreading you expect on the thread that owns the child window.

You're _far_ better off with a solution like the MTMDI Sample presents: let the main thread of your application handle the MDI frame window--and even the view. Then, create a child window of the view on a new UI thread.  This creates an extra window, which is also a child--but that child isn't directly involved in the MDI scheme and therefore won't so frequently block on UI messages all the time.  Furhter, it doesn't require any workarounds and results in far simpler and smaller code.

B ekiM


Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ODBC Connection Logging, ADO.NET 6 73
sumDigits challenge 9 178
bigDiff challenge 17 142
I could not set window to top 4 62
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

734 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