VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Simple technique for avoiding infinite loops that often come up in event-based programming.

by Chris Beckingham (6 Submissions)
Category: Miscellaneous
Compatability: VB 6.0
Difficulty: Unknown Difficulty
Originally Published: Thu 6th December 2001
Date Added: Mon 8th February 2021
Rating: (1 Votes)

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.



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.

Download this snippet    Add to My Saved Code

Simple technique for avoiding infinite loops that often come up in event-based programming. Comments

No comments have been posted about Simple technique for avoiding infinite loops that often come up in event-based programming.. Why not be the first to post a comment about Simple technique for avoiding infinite loops that often come up in event-based programming..

Post your comment

Subject:
Message:
0/1000 characters