UI Not Responding During Nested For Loops

Posted on 2006-11-25
Medium Priority
Last Modified: 2012-05-05

I have an outer For loop that runs 1000 times, and an inner For loop that will run about 3000 times, about 3 million times in all. About 10% of the way through it, the UI stops responding. The calculations keep running and eventually it's responsive again, but the problem is the progress bar locks up and a user would have no real way of knowing if the program is frozen or just too busy to update itself.

I tried making the progress bar separate from the main UI (a separate window pops up with a new progress bar) but that locks up too. It's only called 1000 times from the outer loop...so I don't know why it locks up.

I guess there is just something wrong with the design here. Can anyone offer advice on how to effectively use a progress bar in such a long calculation?

Thanks in advance!
Question by:YoungBonzi
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
LVL 48

Expert Comment

ID: 18013662
Managed or unmanaged C++ ?

Author Comment

ID: 18013671
Hi AlexFM, it's all managed.
LVL 48

Accepted Solution

AlexFM earned 2000 total points
ID: 18013688
Cheap and fast solution: use Application::DoEvents function.

while ( ... )    // long loop
    // calculations

    // update progress bar ...
    Application::DoEvents();               // screen is redrawn here

Working with Application::DoEvents, you need to know that it handles all events, including user commands, not only Paint event. Therefore, you must disable all UI elements which can give unpredictable results, like closing form in the middle of calculation.

Full solution is using worker threads. See BackgroundWorker class.
On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.


Author Comment

ID: 18013735
Haha...That works =]

why is it always one line of code that has me stumped? It's MUCH slower than when not responding, but it works. Would using the BackgrounWorker class speed things up?

Thanks AlexFM =)
LVL 48

Expert Comment

ID: 18013768
It is slower because now application really redraws the screen on every interation. With BackgroundWorker it will be the same. Consider refresing screen once per number of iterations.
Advantage of worker threads is that they are more smart. For example, with DoEvents, you can run calculation and user must wait. With worker threads, you can run calculations while user can continue working with program. DoEvents is usually used in simple situations.

Author Comment

ID: 18013776
Alright...looks like good stuff to know. I'll probably be asking questions abut threads soon =p

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following diagram presents a diamond class hierarchy: As depicted, diamond inheritance denotes when two classes (e.g., CDerived1 and CDerived2), separately extending a common base class (e.g., CBase), are sub classed simultaneously by a fourt…
In Easy String Encryption Using CryptoAPI in C++ (http://www.experts-exchange.com/viewArticle.jsp?aid=1193) I described how to encrypt text and recommended that the encrypted text be stored as a series of hexadecimal digits -- because cyphertext may…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question