on 12/30/01 4:21 PM, Charles Yeomans at yeomans at desuetude dot com wrote:
>> Yeah, but if a window refuses to close, then the loop will execute
>> forever.
>> Bad idea.
>>
>>
>
> Okay, since you brought it up, here's how you reliably handle this
> problem. The problem is this: you'd like to loop through all open
> windows and close them. One way to do this is to include a sentinel in
> the loop to guard against infinite loops. For instance:
>
> dim loopSentinel as Integer
>
> loopSentinel = 0 //yes, I know RB initializes variables;
> //I'm making it explicit for readability
> While (WindowCount > 0) and (loopSentinel < WindowCount)
> Window(0).Close
> loopSentinel = loopSentinel + 1
> Wend
Seems to me there is still a problem (albeit, not with stopping).
Case 1: Assuming windows close as expected.
Suppose we start with WindowCount=2. The first time "into" the loop we have
WindowCount=2 and loopSentinel=0 and the loop is completed. The next time
into the loop WindowCount=1 and loopSentinel=1, the loop is skipped and the
windows stop closing.
Replacing "loopSentinel < WindowCount" by "loopSentinel <= WindowCount" only
delays the problem.
Case 2: Some window refuses to close.
A concrete worst case scenario: Suppose WindowCount=10, w is window(0) and w
refuses to close. Then the loop is perform 10 times without closing any
windows.
A possible solution: close windows in a loop until all are closed or one
refuses to close. I think the following does that:
dim loopSentinel as Integer
loopSentinel= WindowCount
While (WindowCount > 0) and (loopSentinel = WindowCount)
Window(0).Close
loopSentinel = loopSentinel - 1
Wend
The above should terminate with either
1) WindowCount = 0 (successful closures) or
2) loopSentinel < WindowCount (closures failed)
Best regards,
John Roberts
mailto:jarobe01 at athena dot louisville dot edu
|