Solved

UI Not Responding During Nested For Loops

Posted on 2006-11-25
6
236 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
  • 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now