Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
May 18, 1999



Building the Right Environment to Support AI, Machine Learning and Deep Learning

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
	End If
	Executing = False
End Sub
Ron Schwarz
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date