Re: CInternetSession
On Sun, 27 May 2007 18:28:53 -0400, Joseph M. Newcomer
<newcomer@flounder.com> wrote:
ARGH! I'd missed that infinite loop! This is clearly a bad design.
In this case, the presence of the Sleep(10000) indicates the serious design flaw. The
correct model would be to simply use an OnTimer event that would trigger the next
iteration, and use a timer interval of 10000 ms.
It never ceases to amaze me how programmers try to cram everything into the single-thread
sequential-execution model they learned in their freshman programming course. I spend a
lot of time in my courses teaching students to think asynchrously and event-driven.
My standard advice: "If your multithreaded system won't work without liberally tossing
Sleep() calls around like pixie dust, your design is fundamentally broken and will need to
be corrected. If you use Sleep() in a loop, your design is probably wrong and needs to be
corrected. If you use Sleep() in the main GUI thread, your design is flawed beyond
recovery and needs to be rewritten. Assume that, with incredibly rare exceptions, Sleep()
is always a mistake."
I do this after one of the labs fails and can be fixed by tossing a Sleep() call at it. I
let them do a Sleep(10000). Lo and behold, the program works!
The OP is not claiming that Sleep fixes his code.
Then I explain why this
shows that their fundamental design was wrong and then I explain why this solution is
wrong, and challenge them to come up with a guaranteed *correct* solution. After they
finally figure out WFMO, I then explain why IT has problems, and why blocking the main
thread is going to be a complete disaster. From this we work up to asynchronous
reference-counted notificaitons. Then we discuss robustness issues with that approach.
joe
That's a close enough approximation to reality to suffice for nearly all programming
tasks. And it gives them a simple predicate that even low-experience programmers can
test: if there's a Sleep, it's wrong. The number of exceptions to this rule is
vanishingly small.
joe
It seemed clear to me the code you're talking about was just test code
constructed to explore the problems in the real code. The OP was probably
using the Sleep call to slow things down so he could more easily review the
debugger output messages and observe the memory leaks, perhaps in Task
Manager. Using Sleep is not likely related to either problem he described,
nor is it likely to help him discover the cause of his problems. I
described a couple of things that should help in my last message.
--
Doug Harrison
Visual C++ MVP