0

when the program is doing calculations in c++,c#,vb.net you can use the application::doevents() to refresh the screen so the screen do not freeze. how can you do this in assembly particular masm

I tried putting

invoke SendMessage, hWin, WM_SETREDRAW, 0, 0 
invoke SendMessage, hWin, WM_SETREDRAW, 1, 0 

in the loop part of my program but it does not work.

I also tried

invoke SendMessage, hWin, WM_SETREDRAW, 1, 0

but that does not work either

1 Answers1

1

Application::DoEvents does a lot more than just refresh the screen. It actually pumps the message queue for the application's UI thread. If you wanted to do this in assembler, you would need to pump the message queue yourself. However, I don't recommend this. Using DoEvents in any UI framework is a definite anti-pattern, whether it provides such a function or not. More information is here and here.

If all you want to do is ensure that your listbox control gets repainted, then you need to invalidate it and force a redraw. In WinForms, that would be:

myListBox.Invalidate();
myListBox.Update();

In assembler, you will need to invoke the appropriate Win32 API functions yourself. In particular, you can use InvalidateRect to invalidate the client area of your listbox control and then UpdateWindow to ensure that it gets repainted immediately.

I don't know the syntax for MASM, but in unmanaged C++, it would look like this:

InvalidateRect(hListBox,   /* handle to your listbox control    */
               NULL,       /* invalidate the entire client area */
               TRUE);      /* erase the background              */
UpdateWindow(hListBox);

In most cases, invalidating the window is enough. You don't need to force an immediate repaint, because the next time that your application processes messages (in its regular message loop), it will process a WM_PAINT message and redraw the control. The only reason this wouldn't work is if you were blocking the UI thread by performing some type of long-running operation, which you should not be doing in the first place—spin off a background thread to perform calculations.

The reason sending a WM_SETREDRAW message doesn't work here is because that basically sets a flag indicating whether the control is allowed to redraw itself. As the linked documentation explains, the window still needs to be redrawn in order to see the changes. It says you can force this by calling, e.g., RedrawWindow or doing the same thing we did above.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • I'm sorry it is for the whole window not just the list box. I tried putting this code in the loop invoke InvalidateRect,hWin,NULL,TRUE invoke UpdateWindow,hWin but now the screen refreshes and flickers and it does not appear to unlock the screen because when you click a text box the program still locks and crashes – patchariadog Apr 03 '13 at 00:11
  • UGH, do you really want to update the screen with each loop iteration? `WM_SETREDRAW` == FALSE before your loop and `WM_SETREDRAW` == TRUE with `RedrawWindow` after you loop. If you want to update the GUI while in the loop, add your loop code to a thread. – Gunner Apr 03 '13 at 00:35
  • its not that I want to update it its just that I don't want it to freeze I want the person to be able to click on the form with out it locking up. if their is a good way of doing this please let me know – patchariadog Apr 03 '13 at 00:55
  • I tried adding the redrawwindow to my code but it does the same thing. I dont need to update the screen after the loop because it will automatically do that for me. I just need something like c++ application::doevents that I can stick in my loop so the screen does not say "not responding" and locks up – patchariadog Apr 03 '13 at 01:09
  • @patchariadog No, it doesn't "unlock" the screen. The computer can only do one thing at a time. If you are doing work on the UI thread, the UI will be suspended until your work gets finished. That's why I told you to spin off a background thread to do work, and not to do it on the UI thread. That's the only way to prevent the UI from freezing. You can temporarily stop the work and force the window to be redrawn, but like you said, there's the possibility that it will flicker and flash. I don't know what else you expected. You're forcing a repaint. That's the definition of flicker. – Cody Gray - on strike Apr 03 '13 at 02:55