Question

mfc dialog in its own thread

Asked by: dhenderson12

I have a dialog based application in c++ 2008.  I have a dialog that takes some time loading data, so I created a child dialog that displays a single static text control with the text "Loading Data" displayed when the parent form opens.  When the data is loaded, the parent form hides the child dialog ( m_child.ShowWindow(0) ).

There is a picture control on the child dialog that displays an animated gif.  The problem is that the animation won't start until after the loading of the data is complete.  I'm thinking I need to have this dialog running in another thread, but don't know how to accomplish this.

Any help or suggestions appreciated.

This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.

Subscribe now for full access to Experts Exchange and get

Instant Access to this Solution

  • Plus...
  • 30 Day FREE access, no risk, no obligation
  • Collaborate with the world's top tech experts
  • Unlimited access to our exclusive solution database
  • Never be left without tech help again

Subscribe Now

Asked On
2009-10-18 at 09:28:34ID24821775
Topics

Microsoft Visual C++

,

Windows MFC Programming

Participating Experts
3
Points
500
Comments
25

Trusted by hundreds of thousands everyday for fast, accurate and reliable tech support.

  • "The time we save is the biggest benefit of Experts Exchange to Warner Bros. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange." Mike Kapnisakis, Warner Bros.
  • "Our team likes having a resource that is more secure than just using Google and most experts using this service really know their stuff. It's nice to look here first versus using Google." Dayna Sellner, Lockheed Martin
  • "Anytime that I've been stumped with a problem, 9 out of 10 times Experts Exchange has either the accepted solution or an open discussion of the potential solution to the problem." Kenny Red, eBay Inc.

See what Experts Exchange can do for you.

Got a question?

We've got the answer.

Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.

Screenshot of Experts Exchange Knowledgebase

Need individual assistance?

Our experts are ready to help.

If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.

Screenshot of Experts Exchange Knowledgebase

Want to learn from the best?

Read articles from industry experts.

Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.

Screenshot of an Article

Working on a long term project?

Store your work and research.

Save solutions to your questions, answers you’ve discovered through searching plus helpful articles in your personal knowledgebase for easy future access.

Screenshot of Experts Exchange Knowledgebase

Access the answers to your technology questions today.

Subscribe Now

30-day free trial. Register in 60 seconds.

What Makes Experts Exchange Unique?

Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Trusted by the world's most respected brands.

image of each brand's logo

Faithfully serving IT professionals since 1996.

Experts Exchange Logo

Try it out and discover for yourself.

Subscribe Now

30-day free trial. Register in 60 seconds.

Related Solutions

  1. MFC animation
    How can we do animation(of a gif or bmp image) in MFC
  2. Animated GIF
    How can I put (and play) an animated gif image on my window and dialog ? Please be specific.
  3. How to play animated gifs ?
    I would like to know how to play animated gifs within an MFC app ? Thanks in advance.
  4. How to play an animated gif on a dialog?
    I want to put an animated gif on a modal dialog. I'm using Vusial C++ 6.0, Windows 95/98/NT. How can I do that?
  5. display animated gifs
    How can we display animated gifs ?

Free Tech Articles

  1. WARNING: 5 Reasons why you should NEVER fix a computer for free.
    It is in our nature to love the puzzle. We are obsessed. The lot of us. We love puzzles. We love the challenge. We thrive on finding the answer. We hate disarray. It bothers us deep in our soul. W...
  2. SCCM OSD Basic troubleshooting
    SCCM 2007 OSD is a fantastic way to deploy operating systems, however, like most things SCCM issues can sometimes be difficult to resolve due to the sheer volume of logs to sift through and the dispe...
  3. Migrate Small Business Server 2003 to Exchange 2010 and Windows 2008 R2
    This guide is intended to provide step by step instructions on how to migrate from Small Business Server 2003 to Windows 2008 R2 with Exchange 2010. For this migration to work you will need the fo...
  4. Create a Win7 Gadget
    This article shows you how to create a simple "Gadget" -- a sort of mini-application supported by Windows 7 and Vista. Gadgets can be dropped anywhere on the desktop to provide instant information, ...
  5. Outlook continually prompting for username and password
    There have been a lot of questions recently regarding Outlook prompting for a username and password whilst using Exchange 2007. There are a few reasons why this would happen and I will try to cover t...
  6. Backup Exchange 2010 Information Store using Windows Backup
    There seems to be quite a lot of confusion around the ability to backup Exchange 2010 using the built in Windows Backup feature. This stems from the omission of this feature prior to Exchange 2007 s...

Cloud Class Webinars

  1. Avoiding Bugs in Microsoft Access
    Alison Balter takes and in-depth look at avoiding bugs in Access. In this webinar you will learn about using the immediate window to debug your applications, invoking the debugger, using breakpoints to troubleshoot, stepping through code, setting the next statement to execute, ...
  2. Top 10 Best New Features in Visio 2010
    Scott Helmers gives live demonstrations of the top 10 new features in Visio 2010. This webinar will teach you how to create compelling diagrams by adding shapes to the page with a single click, linking the shapes in a diagram to data in Excel (or SQL Server, or SharePoint), ...
  3. IT Consultant Business Secrets Revealed
    Michael Munger, Experts Exchange tech pro and IT consultant, pulls back the curtain on his very successful businesses and answers question on every IT consultant and business owner should know about. He shares secrets on what he did to solve the 5 most common problems in IT, ...
  4. Disaster Recovery and Business Continuity
    Quest CTO, Mike Billon, gives an overview of the steps involved in building a dunamic disaster recovery plan. Through case studies and an examination of software/hardware tooles for monitoring and testing, you'll gain a better understandin of where you are, where you want ...
  5. Organize Your Visio Diagrams with Containers and Lists
    Scott Helmers uses cross functional flowcharts, wireframe diagrams, data graphic legends and seating charts to teach you: how to ustilize all three new structured diagram components in Visio 2010, the best practices for organizeing shapes in previous version of Visio, how to organize ...
  6. How to Us Objects, Properties, Events and Methods in Microsoft Access
    Alison Dalter gives an in-depbth look at objects, properties, events and methods in Microsoft Access. In this webinar you will learn about using the object browser, referring to objects, working with properties and methods, working with object variables, understanding the ...

Join the Community

Give a Little. Get a Lot.

Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.

Join the Community

Answers

 

by: AndyAinscowPosted on 2009-10-18 at 10:47:59ID: 25600660

Have the other dialog in a UI based thread (not a worker thread).

 

by: dhenderson12Posted on 2009-10-18 at 11:00:47ID: 25600701

Is there an example of doing this for a dialog application?  The only examples I can find are for MIDI applications.

 

by: pgnatyukPosted on 2009-10-18 at 11:26:35ID: 25600815

If you want to put a dialog in the thread, just create the thread and in the thread function write:
CMyDialog* pDlg = new CMyDialog;
pDlg->DoModal();
delete pDlg;

Of course, you can use pDlg->Create() and pDlg->ShowWindow(SW_SHOWNORMAL);

That's all.  And maybe this is a mistake.

I'd propose to make one function that will load all data. For the beginning, call this function from OnInitDialog or whenever it is now. This function ONLY loads the data and does not interact with GUI.
Next step will just to create the thread and move the code from this loading function to this thread.
When the data will be loaded, in the end of the thread function post a message to the GUI. GUI, when receives this message, shows the images or whatever it is.

 

by: dhenderson12Posted on 2009-10-18 at 11:45:57ID: 25600876

OK, but how do you create the UI thread?

 

by: pgnatyukPosted on 2009-10-18 at 12:23:08ID: 25600998

DanRollins "Simple Multithreading in Visual C++":

http://www.experts-exchange.com/articles/Programming/System/Windows__Programming/Simple-Multithreading-in-Visual-C.html

"Multithreading -- Why and When":

http://www.experts-exchange.com/articles/Programming/System/Windows__Programming/Multithreading-Why-and-When.html

I like Win32 API: CreateThread. It is just trivial

DWORD WINAPI ThreadFunc(LPVOID pParam)

{

 CParentClass* pWnd = (CParentClass*)pParam;

  CMyDialog* pDlg = new CMyDialog;

 //bla-bla

  pWnd->PostMessage(THREAD_FINISHED);

  return 0;

}

Somewhere in the app:

CreateThread(..., ThreadFunc, this,...);

 

by: pgnatyukPosted on 2009-10-18 at 12:26:41ID: 25601016

BTW, UI thread, communication thread, listening thread,... these are only names.
If there is a GUI window created and handled in a thread function, probably, this thread can be named as an UI thread.

Actually, UI thread, I think, is the main application thread (the first one created in the process), because the main application window is in this thread.

 

by: AndyAinscowPosted on 2009-10-18 at 13:22:43ID: 25601241

Well, if you have an example for an MDI application then you have an example for a dialog based application - creating a UI Thread is exactly the same for both instances.

 

by: dhenderson12Posted on 2009-10-18 at 13:30:11ID: 25601282

According to both articles, he is recommending to stay away from threads unless absolutely necessary.  OK, I'll buy that.  I tried using your example of the api thread, but I'm not understanding something.  Is this declared in the parent dialog or the child dialog?

Is there a way I can do this with a timer?  It seems like a simple proposition, but I'm just not understanding.  Let me try again here and you can show me where I'm going wrong.

1.  I have a main dialog, a child dialog, and a "splash" dialog for the child dialog.
2.  The child form is created on the OnInitDialog method of the main dialog and it's set to be hidden ( ShowWindow(0) ).
3.  When it's time to show the child dialog, from a button event, I set m_childDialog.ShowWindow(1) which shows the chld dialog and the child dialog "splash" dialog which is set to be a child of the child dialog and is visible, so in the OnInitDialog of the child dialog I have m_Splash.ShowWindow(1) .
4.  The main dialog calls a method of the child dialog to load its data which, in turn, is loaded into a group of controls on the child dialog.
5.  THE PROBLEM:  The  "splash" dialog has an animated gif image, but the animation doesn't work until the method that is retrieving the data and loading the child dialog is complete, after which I will close, or hide, the splash dialog.

I may be approaching this completely wrong, so if you have a better method please let me know.  But I would like to have the animation run while the data is being loaded and the child dialog controls are being populated.

 

by: pgnatyukPosted on 2009-10-18 at 13:48:48ID: 25601355

Ok. As you said - it is almost right.
Almost any application has a similar structure.
I proposed you a standard way:
1. Create main dialog
2. From OnInitDialog create the splash dialog with a timer for the animation.
3. From OnInitDialog create the thread loading the data.
4. When the thread finished to load it posts a message to the main dialog.
5. When the main dialog receives this message from the thread, the main dialog closes the thread handle and create the child dialog
6. The child dialog shows the loaded data.

When you create the child dialog invisible - you are wastng the time and resources - you don't know if the data will be loaded correctly or not. So better to wait till you will know what's going with data loading.
Also you are wasting the CPU time - instead of the loading the data as fast as possible, it creates a dialog.

So if to load the data takes longer than a second put the loading into the thread - the GUI will response all the time and will not stuck.

You can pass the pointer to the main dialog to the thread function. So from the thread you can send notifications to the main dialog.

The thread function should be declared as a static function in the main dialog or somewhere else without any class. I'd make it in the main dialog class:
static DWORD WINAPI Loading(LPVOID pParam);
if you will use CreateThread Win32 API.
You will need to add few messages to your application. For example, WM_LOADED. Maybe WM_LOADING_PROGRESS?
The main dialog will handle this message. In MFC ON_MESSAGE(WM_LOADED, OnLoaded) and so on.

Of course, you can solve the task without the thread, but this way will be more complicated and, probably, depends on the data you loading.

 

by: dhenderson12Posted on 2009-10-18 at 14:36:42ID: 25601540

Ok, so just to be clear, I create the child dialog when the main dialog starts up.  It's not doing anything but sitting there doing nothing.  Data for that form won't be loaded until much later in the program.

Since the child method to load the data is being called from the parent, is that where I put the thread call?  or do I put it in the child dialog itself and have it called from the method to load the data?

If this is a static call, do I place it at the top of the .cpp file just below the #include statements, or do I need to create another non-class file for it?

 

by: pgnatyukPosted on 2009-10-18 at 14:42:49ID: 25601556

yes

 

by: dhenderson12Posted on 2009-10-18 at 14:43:50ID: 25601559

yes, what?

 

by: pgnatyukPosted on 2009-10-18 at 14:44:02ID: 25601560

The child dialog is not in the thread.

 

by: dhenderson12Posted on 2009-10-18 at 14:46:51ID: 25601572

are we still having the same conversation?  you've got me totally stumped now.

 

by: pgnatyukPosted on 2009-10-18 at 14:54:30ID: 25601592

:)
we are posting in the same time.
I wanted to say, that I'm going to agree with your design, but I don't like the idea to put the child dialog into the thread.
The thread function can be in the beginning of the cpp-file. Like you said. But it will be better if it will be the static member-function of the main dialog.
so:
1. Child dialog is not in the thread
2. Thread function is a static member-function of the main dialog

 

by: dhenderson12Posted on 2009-10-18 at 15:11:48ID: 25601650

ahhh...ok. :)  

Well, the method I need to call is in the child dialog.  So how would I reference that method to call in the thread?
earlier you provided this:

DWORD WINAPI ThreadFunc(LPVOID pParam)
{
       CParentClass* pWnd = (CParentClass*)pParam;
       CMyDialog* pDlg = new CMyDialog;

       //bla-bla

       pWnd->PostMessage(THREAD_FINISHED);
       
       return 0;
}

so would I replace //bla-bla with pDlg->LoadData()?
and would I reference the parent form, where this function lives, with "this"?

 

by: pgnatyukPosted on 2009-10-18 at 15:15:29ID: 25601659

if you need an explanation:
1. Child dialog is a GUI interacting with the user and controlled by the main dialog (the child is a modaless dialog). If it will be in the thread, you will have a problem even with the message handling.
2. If the thread function is a static member of the main dialog class, it will have access to all members of the main dialog. The thread function should receive this pointer as a parameter and the first line in the thread function should be:
CMainDialog* pThis = (CMainDialog*)pParam;

 

by: pgnatyukPosted on 2009-10-18 at 15:16:57ID: 25601664

:)
Almost. I don't understand why you need this line in the thread function:
CMyDialog* pDlg = new CMyDialog;

 

by: DanRollinsPosted on 2009-10-18 at 15:27:19ID: 25601702

I definately agree that the dialog itself should not run in a separate thread.  You'll have nothing but headaches.

The key issue seems to be lack of animation in the animated GIF.  How are you showing the animated GIF?  Are you using a webbrowser control?

Also, please talk a bit more about the process that "loads the data"  Do you have a data-bound grid control that accesses a remote database?  Are you acquiring data from a web page?  What sort of delays are we talking about here?

 

by: dhenderson12Posted on 2009-10-18 at 15:39:16ID: 25601737

The animated gif is loaded into a static picture control, same as a bitmap.  

The controls on the child dialog are mostly combo box controls;  they are populated by a rather long xml string, it takes about 3 seconds to download, parse, and load the controls.  The xml is delivered from a request to the host. This is host is not a web server.

The CMyDialog* pDlg = new CMyDialog; line was there only because that was what you had posted earlier,  I just did a copy/paste.

There are no web controls in play here, only mfc CDialog classes.

 

by: dhenderson12Posted on 2009-10-18 at 15:40:13ID: 25601740

I know I'm probably beating this to death,  but I really am trying to understand.  As you can tell, I not an experienced C++ coder.

 

by: pgnatyukPosted on 2009-10-18 at 15:48:41ID: 25601757

The original question was how to put a dialog into a thread. So I answered.

Then, I want to think so, we made a great deal - we designed a multi-thread MFC application where all GUI is in the main thread and the data loading is in a separate thread. Again, the thread will load data and notify its owner that the data was loaded (or not loaded) and nothing more. In the GUI part of the application the user will see an animation. It's just nice.

Between us, 3 seconds is not a big deal. Many applications need much more time to initialize their data. But, if the main idea of this EE thread is to design a multi-threaded app - we did it.

 

by: pgnatyukPosted on 2009-10-20 at 00:26:23ID: 25611675

I think that the accepted answer is correct.
I don't see a reason to re-open this thread.

 

by: DanRollinsPosted on 2009-11-02 at 19:58:45ID: 25725947

For PAQ searchers:
Here's an article that implements an MFC dialog running on a secondary a U/I thread:

  Create a Dialog in its Own Thread
  http://www.experts-exchange.com/A_1886.html

20120131-EE-VQP-002

3 Ways to Join

30-Day Free Trial

The Experts

98% positive feedback on 31,087 answers since March 2000. angeliii is a Microsoft Most Valuable Professional for his work with MS SQL Server & Develoment.

He has also proven his knowledge of Visual Basic Programming, PHP Scripting and Oracle Databases.

The Experts

97% positive feedback on 10,752 answers since July 2000. lrmoore has more than 18 years experience in the networking industry.

The six-time Mircosoft MVPs specialties include firewalls, virtual private networking, and network management.

Testimonials

"...and excellent source for support... Kind of like having your very own IT dept." Electriciansnet

Testimonials

"I was apprehensive at signing up at first. However... it has already made my life as an IT administrator much easier." JaCrews

Testimonials

"WOW! You guys have great, active, and knowledgeable people on here." moore50

Business Clients

Business Clients

In the Press

"If you’ve got a question... Experts Exchange can supply an answer.”

In the Press

"...an invaluable aid for both IT professionals and those who require tech support."

In the Press

"where IT professionals provide quick answers on just about any topic"

Business Account Plans

Loading Advertisement...