UpdateData ( FALSE ) don`t work in a FOR loop
-
Hi there , I GOT A SIMPLE PROBLEM.I AM COMPILING A VC++ ( 5.0 ) PROGRAM WHERE I AM HAVING A DIALOG BOX APPLICATION.IN THE DIALOG BOX I AM HAVING A EDIT BOX WHER5E I WISH TO DISPLAY THE VALUE OF THE ITERATION COUNT IN THIS EDIT BOX . SIMPLE !!! THE CODE I HAVE WRITTEN ON MOUSE CLICK OF A PUSH BUTTON IS AS FOLLOWS. :: OnPress() { for ( int i=0;i<10;i++) { m_number=i; UpdateData (FALSE ); Sleep( 1000 ); } } m_number is variable associated with the edit box and is integer type.When i run the program only last count i.e 9 is displayed in the edit box.why not all count is displayed in the edit box? Can somebody please guide me?Why this is happening. Thanks in advance.:confused: Lets share our knowledge and make this world even more beautiful.
-
Hi there , I GOT A SIMPLE PROBLEM.I AM COMPILING A VC++ ( 5.0 ) PROGRAM WHERE I AM HAVING A DIALOG BOX APPLICATION.IN THE DIALOG BOX I AM HAVING A EDIT BOX WHER5E I WISH TO DISPLAY THE VALUE OF THE ITERATION COUNT IN THIS EDIT BOX . SIMPLE !!! THE CODE I HAVE WRITTEN ON MOUSE CLICK OF A PUSH BUTTON IS AS FOLLOWS. :: OnPress() { for ( int i=0;i<10;i++) { m_number=i; UpdateData (FALSE ); Sleep( 1000 ); } } m_number is variable associated with the edit box and is integer type.When i run the program only last count i.e 9 is displayed in the edit box.why not all count is displayed in the edit box? Can somebody please guide me?Why this is happening. Thanks in advance.:confused: Lets share our knowledge and make this world even more beautiful.
I believe it has to do with your calls to the Sleep() function. The sleep function was not realy meant to be used in this maner. What the sleep function does is suspend the execution of your current thread. So your edit box wont even have time to update itself before the sleep starts... or even after. I imagine the only thing displayed in you edit box, after 9 seconds, is 9. I'd recomend looking at using a timer, and update the edit box with that. -Ben "Its funny when you stop doing things not because they’re wrong, but because you might get caught." - Unknown
-
Hi there , I GOT A SIMPLE PROBLEM.I AM COMPILING A VC++ ( 5.0 ) PROGRAM WHERE I AM HAVING A DIALOG BOX APPLICATION.IN THE DIALOG BOX I AM HAVING A EDIT BOX WHER5E I WISH TO DISPLAY THE VALUE OF THE ITERATION COUNT IN THIS EDIT BOX . SIMPLE !!! THE CODE I HAVE WRITTEN ON MOUSE CLICK OF A PUSH BUTTON IS AS FOLLOWS. :: OnPress() { for ( int i=0;i<10;i++) { m_number=i; UpdateData (FALSE ); Sleep( 1000 ); } } m_number is variable associated with the edit box and is integer type.When i run the program only last count i.e 9 is displayed in the edit box.why not all count is displayed in the edit box? Can somebody please guide me?Why this is happening. Thanks in advance.:confused: Lets share our knowledge and make this world even more beautiful.
Think : "Message pump". I do not believe that UpdateData forces messages to be processed for the windows in question, thus your edit window is not refreshed until you fall out what ever message handler you are in. In other words, the message pump is suspended while your loop is processing thus the window you wish to display the result in never recieves the message to update itself with the value. You will need to force the message pump to run from within your loop. Try adding a method like this to your dialog, and call it from within your for loop: BOOL CFWThumbsDlg::ProcessMessageQue() { MSG msg; while( ::PeekMessage((LPMSG) &msg, (HWND) NULL, 0, 0, PM_REMOVE )) { TranslateMessage((LPMSG) &msg); DispatchMessage((LPMSG) &msg); } return( TRUE ); }
-
Hi there , I GOT A SIMPLE PROBLEM.I AM COMPILING A VC++ ( 5.0 ) PROGRAM WHERE I AM HAVING A DIALOG BOX APPLICATION.IN THE DIALOG BOX I AM HAVING A EDIT BOX WHER5E I WISH TO DISPLAY THE VALUE OF THE ITERATION COUNT IN THIS EDIT BOX . SIMPLE !!! THE CODE I HAVE WRITTEN ON MOUSE CLICK OF A PUSH BUTTON IS AS FOLLOWS. :: OnPress() { for ( int i=0;i<10;i++) { m_number=i; UpdateData (FALSE ); Sleep( 1000 ); } } m_number is variable associated with the edit box and is integer type.When i run the program only last count i.e 9 is displayed in the edit box.why not all count is displayed in the edit box? Can somebody please guide me?Why this is happening. Thanks in advance.:confused: Lets share our knowledge and make this world even more beautiful.
Hi. Your code would be right for a console (or DOS) app, which is run as a sequence. As mentioned above, you block the message pump, because you do your work in thread which runs the GUI of your app. So you block the whole app! There are some ways to avoid this problem: 1. Use threads Count in a thread and send the changes as a (User-)message to the main window. 2. Use a timer Create a timer when clicked on "Start" and kill it when clicked on "Stop" In the timer routine you can do your UpdateData(). A propos: Never forget to call UpdateData(TRUE) before UpdateData(FALSE) !! Learning to make a good desing will save you a lot of trouble! Bye, Karl