Force form refresh / repaint from inside event handler

Author: Mark Bleyerveld Asked: 2 years 10 months 3 days 19 hours 35 minutes ago Viewed: 644 times Activity: 2 years 10 months 3 days 9 hours 24 minutes ago


Hi there, I have a form with a button that kicks off a tight loop (displaying video frames), and I am having difficulty getting the form to repaint while the loop is running. I know the main form is live while the loop is running because the controls I have in place are functional (accepting input); however they do not refresh when changed (eg a check box changes state and affects the displayed video frames, but it doesn't change how it is displayed on the main form - it always shows as unchecked, even though I know it has changed state). I have tried a Form.Repaint() and a Form.Refresh.() along with an Application.ProcessMessages() on every loop iteration, but it still does not change what is displayed on the form. Does anyone know the correct incantation to get this to work? Thanks Mark

add comment


Answered by Support at Jul 17, 2021, 3:12:08 PM

Hi Mark. I suppose you run your loop in the OnClick button event. Inside any event Repaint() only indicates that repaint is needed and actual repaint occurs only after exiting the event. It means you need to run loop outside an event. You may use tasks for that. Use AddTask() method of a form as described here:

Thanks for the prompt response, unfortunately the library that I use to display the video frames (opencv) does not work properly in a thread; if the CreateTask function uses threads then this will not work for me. I will try it and see how I go. — by Mark Bleyerveld at Jul 17, 2021, 3:24:17 PM
CreateTask() function does not create any thread. It works in the same thread entire application work. So it should work fine with OpenCV. — by Support at Jul 17, 2021, 3:28:51 PM
Worked (almost) perfectly, thank you! — by Mark Bleyerveld at Jul 18, 2021, 1:10:22 AM

add comment