Solved

UI Not Responding During Nested For Loops

Posted on 2006-11-25
6
271 Views
Last Modified: 2012-05-05
Hello,

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!
0
Comment
Question by:YoungBonzi
[X]
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
6 Comments
 
LVL 48

Expert Comment

by:AlexFM
ID: 18013662
Managed or unmanaged C++ ?
0
 
LVL 8

Author Comment

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

Accepted Solution

by:
AlexFM earned 500 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.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 
LVL 8

Author Comment

by:YoungBonzi
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 =)
0
 
LVL 48

Expert Comment

by:AlexFM
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.
0
 
LVL 8

Author Comment

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

Featured Post

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

738 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