Avoid Unwanted Recursion From Event Cascades

Avoid Unwanted Recursion From Event Cascades

Sometimes, an event might fire while it’s already executing from a prior invocation. To prevent this unwanted behavior, assign a static variable within the procedure, and test it before allowing any more code in the procedure to execute. Then set the variable to True at the start of the main block of the procedure code. When your code finishes, set it to False. This prevents a new instance of the procedure from being invoked while it’s already executing. You might want to add additional code in the test block to deal with situations where you need to do more than simply cancel the execution:

 Private Sub Form_Resize()	Static Executing As Boolean	If Executing Then		Exit Sub	End If	Executing = True	If Width > 6000 Then		Width = 6000		GoDoSomeStuff	End If	Executing = FalseEnd Sub


Share the Post: