Solved

UI Not Responding During Nested For Loops

Posted on 2006-11-25
6
256 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

776 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