Modeless Dialog - Handling dialog refreshing while processing

I have a modeless dialog created in MFC.  The dialog will be performing
intensive processing for a period of time and updating progressbar
controls within itself.

When I launch the modeless dialog, its starts its processing but never
updates the display or handles window events (e.g. moving the dialog).
My guess is the dialog cannot handle window events if it is crunching
away in a loop.

What is the best means to accomplish this scenario?  I'm trying to
avoid threading, but should I use a worker thread?  Do modeless dialogs
have a seperate thread for messaging and processing already implemented
internally?

Thanks.

0
12/2/2005 6:14:35 PM
vc.mfc 33608 articles. 0 followers. Follow

16 Replies
608 Views

Similar Articles

[PageSpeed] 30

The correct way is to implement a worker thread so that your UI is not
locked when you are processing. Look at codeguru/codeproject for
samples. Also take a look at Joe Newcomer's site for issues/samples
related to threading in MFC.

----------
Ajay Kalra
ajaykalra@yahoo.com

0
ajaykalra (6842)
12/2/2005 6:55:38 PM
Thanks!  I had a feeling this was the way to go, but I wanted to make
sure.

Just out of curiosity, what is the URL for the Joe Newcomer's site?

0
12/2/2005 6:58:08 PM
http://www.flounder.com/mvp_tips.htm

-------------
Ajay Kalra
ajaykalra@yahoo.com

0
ajaykalra (6842)
12/2/2005 7:01:57 PM
This is good as well:

http://members.cox.net/doug_web/threads.htm
http://www.mvps.org/vcfaq/mfc/12.htm

----------
Ajay Kalra
ajaykalra@yahoo.com

0
ajaykalra (6842)
12/2/2005 7:06:12 PM
Thank you!  These are all very good resources.

0
12/2/2005 8:39:38 PM
You could also try a function like this one which allows messages to be 
processed.  You could call this during your intensive times:

//
// Release main thread for background processing
//
void GiveTime()
{
    // Idle until the screen redraws itself, et. al.
    MSG msg;
    while (::PeekMessage( &msg, NULL, 0, 0, PM_NOREMOVE ) ) {
        if (!AfxGetThread()->PumpMessage( )) {
            ::PostQuitMessage(0);
            break;
        }
    }
    // let MFC do its idle processing
    LONG lIdle = 0;
    while (AfxGetApp()->OnIdle(lIdle++ ))
        ;
}

Tom

"Jordan" <jordan.parmer@gmail.com> wrote in message 
news:1133547275.247734.174760@o13g2000cwo.googlegroups.com...
>I have a modeless dialog created in MFC.  The dialog will be performing
> intensive processing for a period of time and updating progressbar
> controls within itself.
>
> When I launch the modeless dialog, its starts its processing but never
> updates the display or handles window events (e.g. moving the dialog).
> My guess is the dialog cannot handle window events if it is crunching
> away in a loop.
>
> What is the best means to accomplish this scenario?  I'm trying to
> avoid threading, but should I use a worker thread?  Do modeless dialogs
> have a seperate thread for messaging and processing already implemented
> internally?
>
> Thanks.
> 


0
tserface (3860)
12/2/2005 8:57:45 PM
One more question...do worker thread functions have to exist outside
the scope of the thread?  In other words, can thread methods be member
functions inside of a class?

0
12/2/2005 8:58:51 PM
Thanks Tom.  This did the trick.

I attempted to be lazy and had the class call a global function passing
in a pointer back to the class so the global function could call a
method in the class.  This didn't work, however, as I remembered that
threads do not share stack space.

So what do most people do in these situations?  Do they give the dialog
a breather to handle messages (i.e. Tom's suggestion) or do they use
worker threads.  The problem I have with worker threads is that I need
to update the progress bars on the dialog inside of the intensive
process.  I guess I could pass the progress bars to the thread via the
LPVOID pParam, but this seems more like a workaround.

0
12/2/2005 10:05:42 PM
>
> So what do most people do in these situations?  Do they give the dialog
> a breather to handle messages (i.e. Tom's suggestion) or do they use
> worker threads.  The problem I have with worker threads is that I need
> to update the progress bars on the dialog inside of the intensive
> process.  I guess I could pass the progress bars to the thread via the
> LPVOID pParam, but this seems more like a workaround.
>

What Tom gave you is a good way to do it.

But if you want to switch to a worker thread:
You should to it the other way around, pass the window handle of the dialog
with the progress control to the worker thread and from the worker thread
post messages to the window telling it to update it's progressbar.

AliR.


0
AliR3470 (3235)
12/2/2005 10:10:44 PM
I think I see what you are saying, but how would the new worker thread
know to update it's progress bars?  Would it sit waiting for a message
from the primary thread of the dialog?

0
12/2/2005 10:32:38 PM
Jordan wrote:
> One more question...do worker thread functions have to exist outside
> the scope of the thread?  In other words, can thread methods be member
> functions inside of a class?
> 

The thread function can be a member function of a class only if it is 
declared as a static function.  When you create the thread you can pass 
a PVOID parameter to the thread function.  A popular pattern is to pass 
'this' to a static thread function, and make it a one line function that 
uses the pointer to call a nonstatic member function.  That gives your 
thread access to all the class members.

-- 
Scott McPhillips [VC++ MVP]

0
Scott
12/2/2005 10:33:18 PM
Scott,

Your the man!  That did the trick.

However, I have one more thing to make your life difficult.  I'm using
COM.

::CoInitialize() is called on the primary thread.  I get an exception
using COM in the worker thread with a return status saying CoInitialize
has not been called.  Would I call ::CoInitialize( ) again in the
worker thread?  Is this a good idea?

0
12/2/2005 10:59:24 PM
One thing I learned the hard way is that you never want to try to call a 
function in another thread that updates any kind of user interface. 
Instead, you can send a message to another window and let it do it for you.

Tom

"AliR" <AliR@online.nospam> wrote in message 
news:ED3kf.24180$7h7.11243@newssvr21.news.prodigy.com...
> >
>> So what do most people do in these situations?  Do they give the dialog
>> a breather to handle messages (i.e. Tom's suggestion) or do they use
>> worker threads.  The problem I have with worker threads is that I need
>> to update the progress bars on the dialog inside of the intensive
>> process.  I guess I could pass the progress bars to the thread via the
>> LPVOID pParam, but this seems more like a workaround.
>>
>
> What Tom gave you is a good way to do it.
>
> But if you want to switch to a worker thread:
> You should to it the other way around, pass the window handle of the 
> dialog
> with the progress control to the worker thread and from the worker thread
> post messages to the window telling it to update it's progressbar.
>
> AliR.
>
> 


0
tserface (3860)
12/2/2005 11:41:50 PM
I believe you need to initiliaze COM on that thread as well.

----
Ajay Kalra
ajaykalra@yahoo.com

0
ajaykalra (6842)
12/3/2005 12:42:52 AM
Jordan wrote:
> I think I see what you are saying, but how would the new worker thread
> know to update it's progress bars?  Would it sit waiting for a message
> from the primary thread of the dialog?
> 

Seems kind of backwards.  The worker thread should just casually report 
progress from time to time (say, every 10,000th loop or something).

To report progress it does a PostMessage over to the dialog (or some 
other window).  Here's a how-to:
http://www.mvps.org/vcfaq/mfc/12.htm

-- 
Scott McPhillips [VC++ MVP]

0
Scott
12/3/2005 2:35:37 AM
I'm not sure what you mean here...
On 2 Dec 2005 14:05:42 -0800, "Jordan" <jordan.parmer@gmail.com> wrote:

>Thanks Tom.  This did the trick.
>
>I attempted to be lazy and had the class call a global function passing
>in a pointer back to the class so the global function could call a
>method in the class.  This didn't work, however, as I remembered that
>threads do not share stack space.
***
And your point is?  Why is sharing stack space even relevant here...?
****
>
>So what do most people do in these situations?  Do they give the dialog
>a breather to handle messages (i.e. Tom's suggestion) or do they use
>worker threads.  The problem I have with worker threads is that I need
>to update the progress bars on the dialog inside of the intensive
>process.  I guess I could pass the progress bars to the thread via the
>LPVOID pParam, but this seems more like a workaround.
****
Worker threads of some description.  Due to an unfortunate failure in naming conventions,
there are two kinds of "worker" threads, those without message pumps, which Microsoft
calls "worker threads" and those with a message pump, which Microsoft calls "UI threads".
Both are concurrency mechanisms and both can be used.

Using PeekMessage is considered quaint.  It also limits interactions to the points where
PeekMessage is called; call it too infrequently, and your UI is sluggish; call it too
often and your performance suffers.  Use user-defined messages to send notifications from
the thread doing the work to the main GUI thread.
				joe
****
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
0
newcomer (15972)
12/6/2005 5:06:51 PM
Reply:

Similar Artilces:

Modeless Menu
I'm trying to create a flyout menu similar to Photoshop (not the same as a normal toolbar drop-down) in that I want to be able to capture the mouse-up event on the toolbar while the menu is active. Is there a way to make the Menu Modeless or to capture the message and pass it back to the Toolbar? Is this the sort of thing you're looking for? I think Photoshop does it's settings menu sort of like this one: http://www.codeproject.com/dialog/csettingsdlg.asp If you are just looking for a property sheet type menu perhaps this would work: http://www.naughter.com/treeoption...

Help with Building Template, Repeatable Process
I've got a survey tool that exports survey results as Excel. It puts each question on a seperate worksheet tab. The export always has the same naming scheme and structure everytime. I can't change the survey export. So my tabs looks like: Question_1 Question_2 Question_3.... I created a "Master Summary" worsksheet that summarizes the results of each question tab onto one page. So my cell references look like: ='Question_1'!A6 ='Question_2'!A6 ='Question_3'!A6... The problem is that I cannot come up with a process by which I can export a fresh file...

Issues when pivot charts are refreshed and pivots has filters
Dear Sir, Need help with pivot charts. The problem I am facing is as given. Have created a pivot. It has filters, by name and we want to track for each person productive & non productive hours. One name is chosen and a pie chart is made and formating done with labels (to display the absolute no. and %). Post this when another person is chosen in the pivot with the filter the complete formating goes for a toss. No labels can be seen. What is the way out? Wish you Happy New Year 2010 regards saswato mitra Excel 2007 PivotTable, PivotChart Seems to work in my set...

Format cells dialog
I have a xls file that will not let me do any cell formatting to some cells. I can use the format painter to force changes. The sheet is not protected. I have the same problem whether it is marked locked or not. When I select Format | Format Cells.. , right click the cell and select "Format Cells..", CTRL+1, nothing happens. I can use the Alignment buttons but will never get the Format Cells Dialog Box. Other cells on the same sheet work just fine? I'm using 2007 You say that some cells work OK. Copy those that do work to those that do not and I think...

How to Implement More Button in Dialog Box ?
Hi, I am doing automation in PowerPoint using VC++.I want to know that how to implement More button. I mean to say is that when I click on More Button the size of the Dialog Box should increase & MORE button should change to LESS and Vice-versa. This is same button that you might see in MS Word's "Replace Utility". If youhave a look on it you will be able see what exactly I mean. I want to implement that "MORE" button in my dialog box. But I dont have any idea how to do it in Visual C++, If any one knows how to do it, Please help me in doing this. Thanks A...

terminate a dialog
If a test fails in my OnInitDialog(), I would like to terminate the dialog and continue what I was doing before. How do I do this? I have tried: EndDialog(0); return false; This works, but seems to be a little unstable. How is it unstable? I haven't noticed? If you really don't feel confortable with that then you can set a timer, or use postmessage to yourself from OnInitDialog that would later call EndDialog(IDCANCEL) AliR. "GT" <ContactGT_remove_@hotmail.com> wrote in message news:45d1e39a$0$17294$c3e8da3@news.astraweb.com... > If a test fails in my On...

modeless
I'm sure this gets asked all the time,. but why is it that it is so difficult to create a modeless dialog window? Codeguru.com - Visual c++ Goodies, talksabout how annoying modal dialog windows are, but it doesn't tell you how to create a modeless dialog window. Visual c++ in 21 days, menions modeless dialog window on a single page,again doesn't tell you how to do it. The only place that has any info is MSDN, but I have reall difficulties following the jargon on MSDN. Is there a kind'a "let-me-take-you-by-the-hand-tutorials" on modeless dialog windows anywhere? T...

Outlook 2003 print dialog
Has anyone figured out a fix for the differing print dialog boxes in Outlook 2003? That is, a text only email has a different print dialog box than an RTF or HTML email. Mostly I want to be able to print just the first page, but for text-only email, the print dialog does not offer this. This has been discussed in another post, but with no resolve. http://groups.google.com/groups?selm=403CE6A8.70901%40neo.rr.com Matt There is no way to change Outlook's behavior. You can, of course, save the message as a .txt file and then open it with WordPad and print whichever pages you choose. -- ...

Can you add a user-defined field to the new task dialog box?
Hi All: I use a custom arrangement to view my tasks in which I have a field called "No." (for number). I use it to order the tasks each day. That works fine. However, when I double click a task or if I start a new task with the shortcut key "Ctrl+Shift+K" (or Ctrl+N), a dialog box opens. This standard dialog box for an untitled task doesn't contain a field for my "No.". Is there a way I can modify that dialog box to add a number field that would show up in my custom arrangement under the "No." field? i.e. basically, I want to ad...

Destryoing modeless dialog in a modeless thread causes deadlock
Hello there, I've created a user-interface thread to manage modeless dialog. I use it to display comforting "Please wait..." messages to the user while worker threads do lengthy operations (in this case, tasking robotics, but that's not important). So, here's a use case: The user initiates a long process by selecting a menu item. In the main GUI message handler, a worker thread is created to do perform the operation. The message handler returns. The worker thread creates a user-interface thread, which creates a modeless dialog. Any time the worker thread needs t...

strangely modeless dialog working even when i didnt destroy object
hello, I ahve created a dialog based application.I have inserted 6 menu options voz dialog1,dialog2,dialog3,....dialog6 in the dialog .On clicking of these menu opions i am popping 6 different modeless dialog.here is the code written for it midialog *d;//class for dialog resource d=new midialog; d->Create(IDD_DIALOG1); d->ShowWindow(SW_SHOW); To close it i either click on OK or Cancel or Red cross button and then i close the parent window.It strangely doesn't give an error.I was thinking that for modeless dialog you need to override OnCancel,OnClo...

Location info dialog box
(Win XP, Outlook 2003) - I'm all of a sudden starting to have a problem when I add or change a phone number in a contact. I'm connected via LAN, not dialup, and no settings have changed. Yet when I put in a phone number I get the Location Information dialog box (this box: http://www.babcockancestry.com/storage/outlookproblemlocationinfo.jpg). I can NOT get out of it in any way except using the task manager to End Task, which of course does not save the contact. If I try to cancel in the dialog, it just keeps coming back. Any changes except phone numbers work fine. It's only when I...

Format Cells Dialog Box
Hi, I was delivering Excel training the other day when one of my syudents asked me a question. Can you help answer it. She wanted to know why, on the Format -> Cells dialog box (Patterns Tab) the colours appear in two groups with a horizontal line separating each group. My theory is that it is something to do with different colour palettes or colour depth. Anyone know any better ? Regards eyrei@aol.com Good question. I've occasionally sort of wondered about that too but not enough to investigate <g>. Just did some digging: Look at Tools>Options>Color. This...

Paint a colored rectangle in a dialog?
Hi How can I paint a colored rectangle in a dialog? Thanks Urban "Urban Stadler" <mail@ustadler.net> wrote in message news:3f621e6a$0$44078$91cee783@newsreader02.highway.telekom.at... > How can I paint a colored rectangle in a dialog? CDC::FillSolidRect. Jeff... Create a subclassed CStatic object (that is, create a CStatic and a CStatic-derived subclass, and add a control variable of the CStatic subclass). In the OnEraseBkgnd handler, paint the client area: BOOL CMyStatic::OnEraseBkgnd(CDC * pDC) { CRect r; GetClientRect(&r); pDC->FillSolidRe...

modeless MDI
Oh heck..., This is what I'm trying to do. What's the best way forward? I want to have a "CONTROL" panel. Which contains a list of objects. I wanto be able to click on the objects to open a window to edit its properties. I'd liike to be able to open ultiple nstances of the window. I guess this sounds like an MDI? I want also to be able to click on bits of the control panel to open up modal dialog winodws to edit other properties, e.g. add another item to the list. I have a feeling that my mouth is bigger than my stomach. I'm not even meant to be a programmer.... I...

menu in dialog stale application
Hi, I wrote simple app with menu IDR_MAIN3 on the top (File .... HELP etc.), and now it looks: IDD_M_DIALOG DIALOGEX 0, 0, 571, 427 STYLE DS_SETFONT | DS_MODALFRAME | DS_3DLOOK | WS_MINIMIZEBOX | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU EXSTYLE WS_EX_ACCEPTFILES | WS_EX_APPWINDOW CAPTION "simple dialog" MENU IDR_MAIN3 FONT 8, "MS Sans Serif", 0, 0, 0x1 .... but when I running my app, menu is not on the same plane with the rest of dialog, this app is not flat, and I dont know why? m. I dont get what you are saying. What do you mean by same plane? Do you mean di...

Modeless PropertySheet
Hello: I have modeless PropertySheet and I want it to be in the same location. Which handler should use ? thank you, dave > I want it to be in the same location. Same location as what? Across sessions? What type of application is it? --- Ajay > > Same location as what? Across sessions? What type of application is it? > > --- > Ajay It's a SDI application. And I want to have modeless PropertySheet located by left border. Thank you, Dave Pass the handle of the view to the property sheet. Then move it if you need to m_PropertySheet.SetWindowPos(NU...

Change to a Dialog
Hi Guys, I have a tabbed dialog, I have 2 tabs on it. That means each of the tabs are derived from CDialog in the code generated by mfc. I want the tabs to be derived from CPropertyPage. Is there a way of changing this via classwizard or is making the changes by editing the code only option? Thanks Vipin The easiest way (in my opinion) would be to use the ClassWizard to generate new classes (either use new class names, or rename the old ones first) derived from CPropertySheet and CPropertyPage. You can then copy and paste the relevant code from the old classes into the n...

attaching child ClistCtrl window to resizable parent Dialog
I'm using Visual C++ 7.0 and MFC With the Wizard, I created a Dialog Window [which I made resizable]. I've place two ClistCtrls side by side on the top of the dialog. I'd like to attach the RHS of the rightmost ctrl to the parent dialog so that when I resize the dialog, the listctrl expands. Note that I don't explicitly create the listctrls - this ia all behind the scene. I only send an initial message to each: m_ConfCtrlList.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE,0, LVS_EX_FULLROWSELECT|LVS_REPORT|LVS_EX_GRIDLINES ); Thanks, Ihor VC++ doesn't not...

MSACCESS.exe process doesn't stop
The msaccess doesn't stop running when I close my application. The problem occurs when I open a specific subform and is only occuring since I've added a new subform to said subform. If I remove the newly added subform the problem goes away. The only reason I can think could be the cause is that the new subform is not linked to to any other form. Any suggestions? TIA, Tim J Are you setting all of your set objects back to Nothing? Tim J wrote: >The msaccess doesn't stop running when I close my application. > >The problem occurs when I open a specific subform and...

opening CFileDialog from modeless or creating modeless CFileDialog
Hey everyone, got a quick questions again as I went and messed up my MFC app and now can't seem to find a solution to my problem. My Old program was simply 1 modal dialog box (call it EDITOR) and when the user clicked "open" it called CFileDialog (of course domodal()) and everything worked fine. Now however I have a different dialog box as the modal, which in turn CREATEs (modeless :) my EDITOR. (This so far of course works fine). However whenever I click on the "open" button the program immediately crashes (bringing up dbgrpt.c) basically telling me that the CFile:...

Easily disable multiple controls in dialog box
How can I easily disable/enable a bunch of controls on a dialog box? I have a long series of settings that I want to tie to a single check box as to whether they are enabled or not. And I'd rather not create a dozen or more member variable for each one and hard code calling EnableWindow on each one. I'd like something like the Panel object in C++Builder, that I can just put all the controls on that and call that objects EnableWindow to do them all. Is there anything like this in MFC? -- Jonathan Arnold inSORS "Jonathan Arnold" <jdarnold_online@insors_comm.com> wrot...

Network Path/Excel Open Dialog Problem
Greetings. I am using Excel's open dialog to locate a file that needs to be imported. The only way I have found to set the default directory for this dialog (so it is in the directory I want when the dialog opens) is to change the current directory to the desired directory, open the dialog and locate the file and then put the current directory back to the orignal directory when the dialog closes. Too much directory hopping, if you ask me... Is there an easier way to do this? Hi but this is the way how you have to do it :-) -- Regards Frank Kabel Frankfurt, Germany "Nny...

Modeless dialog
This is a multi-part message in MIME format. ------=_NextPart_000_0008_01C34B03.2FC46110 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hello, I have a dll containing my resources. I have a class CSplashDlg : public CDialog the CSplashDlg resource is in the dll and called IDD_SPLASH_DIALOG it contains only a bitmap for the splash screen. I'm trying to show the splash screen while the the loading finishes...so = i have something like that // Main Dlg CDetectionDlg dlg; m_pMainWnd =3D &dlg; // Splash Screen CSplashDlg splash; s...

How to Hide Dialog Application during startup
Hi Group I did a simple MFC dialog. My idea is to run the app in system tray and not to show the dialog to user. I can sucessfully load it at the system tray but my call to ShowWindow(SW_HIDE) returns 0 during OnInitDialog(); Only after the dialog is displayed then if i press a button to hide it it will be sucessfull. So how do i set the dialog to hide during startup. Regards Here is a solution provided by someone else ///////////////////////////////////////////////////////////////////////////////// //hide dialog at startup /////////////////////////////////////////////////////////////////...