Solved

UI Not Responding During Nested For Loops

Posted on 2006-11-25
6
245 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

15 Experts available now in Live!

Get 1:1 Help Now