Re: How to prevent "Not Responding" on lengthy operations
Phil wrote:
How do you prevent a program from looking like it's crashed when it's
doing some number crunching ?
This is an inevitable consequence of performing lengthy operations
without returning to the MFC message dispatcher. If you're calculating
then you're not responding to mouse and paint messages. Updating
windows won't completely fix the problem.
The expert solution is to use multithreading: do the lengthy operation
in a second thread. This requires some expertise with multithreading
including shared data access synchronization and interthread
communication. There are several tutorials on this at codeguru.com,
codeproject.com and flounder.com. Multithreading has a lot of "gotchas"
for the beginner so you face possible risks here.
A less advanced solution is to simply break up the work into short
pieces and use a timer. Do one piece of the work upon each WM_TIMER
message, then return to MFC. This is the K.I.S.S. solution.
Finally, and not recommended, is the old Win 3.1 technique of calling a
message pump periodically from within your lengthy operation loop. (Win
3.1 did not have multithreading.) This works but it's clumsy and
introduces reentrancy snarls that can be hard to anticipate.
--
Scott McPhillips [VC++ MVP]