From: Joseph M. Newcomer on
Actually, for a variety of reasons, this is exceptionally bad code.

First, I believe that using UpdateData at all is a fundamental design error. I have spent
far too much of my life fixing code that uses it, and my "fix" is to always remove it
completely. If you want to set a window to have a value, you will call SetWindowText to
do so.

Second, you are doing this inside some loop. Until you return to the main message pump,
the WM_PAINT message that causes the control to be redrawn will not be sent, so there is
no way to see it change.

Third, the solution, which is to do SetWindowText followed by UpdateWindow, is VERY
expensive, and will substantially delay your loop.

In general, if you have a long computation loop, the correct code would involve creating a
secondary thread to do the computation, and it would PostMessage requests back to the main
GUI thread to request updates of fields.
joe

On Tue, 13 May 2008 03:20:01 -0700, nexolite <nexolite(a)discussions.microsoft.com> wrote:

>I have made a simple MFC(dialog based) program in which the dialog box has
>a CEdit box i have defined a DoDataExchange
>so my problem is whenever I do something like this
> for(i=0;;i++)
> {
> e=i;
> UpdateData(false);
> }
>where "e" is associated with the edit box
>The values as they should be instantaneously displayed in the box the moment
>they change, but nothing happens and dialog box freezes.
>but if I call a MessageBox("fh"); after UpDateData(false) then the message
>box is displayed and the value change is also displayed ,but I want it
>without this message box....plz help me
>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Joseph M. Newcomer on
I missed that. This is worse than just bad code, this is downright incorrect code.
joe

On Tue, 13 May 2008 08:22:56 -0400, David Wilkinson <no-reply(a)effisols.com> wrote:

>nexolite wrote:
>> I have made a simple MFC(dialog based) program in which the dialog box has
>> a CEdit box i have defined a DoDataExchange
>> so my problem is whenever I do something like this
>> for(i=0;;i++)
>> {
>> e=i;
>> UpdateData(false);
>> }
>> where "e" is associated with the edit box
>> The values as they should be instantaneously displayed in the box the moment
>> they change, but nothing happens and dialog box freezes.
>> but if I call a MessageBox("fh"); after UpDateData(false) then the message
>> box is displayed and the value change is also displayed ,but I want it
>> without this message box....plz help me
>
>nexolite:
>
>I would use SetWindowText() here. Also, once your loop starts there is nothing
>to stop it, and you will have to kill the application. Better to use a timer.
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Joseph M. Newcomer on
"Even" calling Sleep? What makes you think that slowing bad code down is going to make it
correct?
joe

On Tue, 13 May 2008 05:05:00 -0700, nexolite <nexolite(a)discussions.microsoft.com> wrote:

>even calling ::Sleep() does not helps!
>what should I do?
>
>"nexolite" wrote:
>
>> I have made a simple MFC(dialog based) program in which the dialog box has
>> a CEdit box i have defined a DoDataExchange
>> so my problem is whenever I do something like this
>> for(i=0;;i++)
>> {
>> e=i;
>> UpdateData(false);
>> }
>> where "e" is associated with the edit box
>> The values as they should be instantaneously displayed in the box the moment
>> they change, but nothing happens and dialog box freezes.
>> but if I call a MessageBox("fh"); after UpDateData(false) then the message
>> box is displayed and the value change is also displayed ,but I want it
>> without this message box....plz help me
>>
>>
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: nexolite on
thnx the idea of using other thread for computation came to my mind but I
thought it as the last option(dont know why!)
but now I am able to do it with SetWindowText() with or without Sleep()
thnx all.

"Joseph M. Newcomer" wrote:

> Actually, for a variety of reasons, this is exceptionally bad code.
>
> First, I believe that using UpdateData at all is a fundamental design error. I have spent
> far too much of my life fixing code that uses it, and my "fix" is to always remove it
> completely. If you want to set a window to have a value, you will call SetWindowText to
> do so.
>
> Second, you are doing this inside some loop. Until you return to the main message pump,
> the WM_PAINT message that causes the control to be redrawn will not be sent, so there is
> no way to see it change.
>
> Third, the solution, which is to do SetWindowText followed by UpdateWindow, is VERY
> expensive, and will substantially delay your loop.
>
> In general, if you have a long computation loop, the correct code would involve creating a
> secondary thread to do the computation, and it would PostMessage requests back to the main
> GUI thread to request updates of fields.
> joe
>
> On Tue, 13 May 2008 03:20:01 -0700, nexolite <nexolite(a)discussions.microsoft.com> wrote:
>
> >I have made a simple MFC(dialog based) program in which the dialog box has
> >a CEdit box i have defined a DoDataExchange
> >so my problem is whenever I do something like this
> > for(i=0;;i++)
> > {
> > e=i;
> > UpdateData(false);
> > }
> >where "e" is associated with the edit box
> >The values as they should be instantaneously displayed in the box the moment
> >they change, but nothing happens and dialog box freezes.
> >but if I call a MessageBox("fh"); after UpDateData(false) then the message
> >box is displayed and the value change is also displayed ,but I want it
> >without this message box....plz help me
> >
> Joseph M. Newcomer [MVP]
> email: newcomer(a)flounder.com
> Web: http://www.flounder.com
> MVP Tips: http://www.flounder.com/mvp_tips.htm
>
From: Doug Harrison [MVP] on
On Tue, 13 May 2008 03:20:01 -0700, nexolite
<nexolite(a)discussions.microsoft.com> wrote:

>I have made a simple MFC(dialog based) program in which the dialog box has
>a CEdit box i have defined a DoDataExchange
>so my problem is whenever I do something like this
> for(i=0;;i++)
> {
> e=i;
> UpdateData(false);
> }
>where "e" is associated with the edit box
>The values as they should be instantaneously displayed in the box the moment
>they change, but nothing happens and dialog box freezes.

Yes, infinite loops that don't process messages tend to make your program
freeze.

>but if I call a MessageBox("fh"); after UpDateData(false) then the message
>box is displayed and the value change is also displayed ,but I want it
>without this message box....plz help me

Don't use UpdateData for a subset of the controls. For more on what
UpdateData is for, and how to use it correctly, see:

http://groups.google.com/group/microsoft.public.vc.mfc/msg/1beb7cdaf010b0c6

--
Doug Harrison
Visual C++ MVP