devxlogo

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

Charlie has over a decade of experience in website administration and technology management. As the site admin, he oversees all technical aspects of running a high-traffic online platform, ensuring optimal performance, security, and user experience.

See also  Five Early Architecture Decisions That Quietly Get Expensive

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.