-2

I have a specialized application that is compute intensive with adjustable and occasional visual feedback into a component on the main form. My main form also has a slider that controls how the user can balance between calculation speed and UI response speed. I know how to create a background worker thread, but that's not my intention, as allowing the UI thread to process messages continuously slows my calculations down by several orders of magnitude. I want to deliberately block the UI thread as controlled by the slider. After a certain adjustable number of loops, I allow the UI to process messages with an Application.DoEvents() instruction in my code and at this time all pending UI updates are processed. I allow the looping to be controlled by the slider. 50 loops before UI freed gives a nice visual response time with no perceivable UI lag, and 2000 loops before UI freed takes about 15 seconds before the UI catches up, but boosts the calculation speed immensely.

That being said, is there any way to avoid a release mode program from cycling back and forth from "Not Responding" to regular mode when the slider is set to block the UI?

Thanks.

Thanks for all your responses. Thanks for your advice. There is a lot of interaction between the calculations and several UI components on the form. I have already tried the background thread with Invoke delegate based templates with extension method as suggested elsewhere on the internet and that works in the manner you suggest, but makes the calculations so slow as to be ridiculous. As a workaround, I can turn the component.visible to false and that only leaves the progress bar to update in real time, and that speeds things up almost as much as maxing out my slider, but it's frankly no fun to watch just the progress bar instead of seeing updates occur on the main visual component every 10 seconds or so. I am pushing a LOT of pixels around, that's all the information I can provide on this application for IP reasons. So my question remains, can I have my cake and eat it too? Can I turn off those windows messages that make my app form blink, or not? I have workarounds, none of which I really like, and comments that don't address the question directly are not useful to me. Thanks for your patience and understanding.

user7191223
  • 33
  • 1
  • 6
  • 2
    A properly designed UI consumes more or less 0% CPU, so something's definitely wrong with your code. What's that *"visual feedback"*? Whatever you're doing, I bet you're giving feedback at every iteration - don't do this. Only update feedback a few times a second, that should be more then enough. – Lucas Trzesniewski Nov 21 '16 at 20:39
  • "a slider that controls how the user can balance between calculation speed and UI response speed" Huh? Why should anyone have to choose between these? – Dan Wilson Nov 21 '16 at 20:41
  • Thank you for your interest. To answer your question, because a properly designed and marketable application doesn't force the programmer's decisions on the user without a built-in recourse. Unless you like issuing updates every week. – user7191223 Nov 21 '16 at 21:17
  • 2
    "allowing the UI thread to process messages continuously slows my calculations down by several orders of magnitude" shows there is something seriously wrong with your UI code. The correct design is the UI thread doing UI, a background thread doing the calculation - no slider needed – pm100 Nov 21 '16 at 21:21

2 Answers2

2

You should implement any heavy calculation as a background thread and synchronize progress/results with the UI.

Going this way you won't never block the UI and heavy calculations will be done while the user can see their progress on the UI.

Also, avoid showing progress on the UI in very small time intervals. 1, 2 or even 5 seconds refreshes might be enough!

I would take a look at this other Q&A: Invoke in Windows Forms. It's old but it should be a good start for you.

Community
  • 1
  • 1
Matías Fidemraizer
  • 63,804
  • 18
  • 124
  • 206
0

Found a solution myself. Thank you everybody for the benefit of your expertise.

I am now doing all of my pixel writes to an image that is not visible. This allows the calculation engine to proceed at maximum speed unhindered by interacting with the UI. Every so often, I assign a clone of the image to the visible component, which provides the real time feedback, but not at the expense of the calculation speed.

Was able to hard-code UI updates at 1000 and got rid of the slider. UI updates occur every 3-5 seconds. Nice.

Much, much better.

user7191223
  • 33
  • 1
  • 6