Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


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

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

 

 

 

 

 


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

 

 

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