- Home
·
- Miscellaneous
·
- Simple technique for avoiding infinite loops that often come up in event-based programming.
Simple technique for avoiding infinite loops that often come up in event-based programming.
Simple technique for avoiding infinite loops that often come up in event-based programming.
Rate Simple technique for avoiding infinite loops that often come up in event-based programming.
(1(1 Vote))
loops is the following:
Public Sub MySub()
CallSomething '<- This method call somehow fires MySub Again, so we get an infinite loop
End Sub
Rewrite like this:
'Module level variable:
Private mbMySubCalled as boolean 'Make sure this gets initialized to false
Public Sub MySub()
If mbMySubCalled = True then
mbMySubCalled = False
Exit sub
else
mbMySubCalled = True
end if
CallSomething
End Sub
What you've basically done is set a flag the first time the sub gets called. The second time in, you look at that flag, if its set, unset it and exit. If not, set it and continue.
Simple technique for avoiding infinite loops that often come up in event-based programming. Comments
No comments yet — be the first to post one!
Post a Comment