Q: I have a For-Next loop where intensive processing takes place. I would like the application to respond other events during execution of this code.
A: The method DoEvents() will handle this requirement. By placing a call to DoEvents at the top of the loop, this method will process all messages queued from the Operating System, including shutting down the application. Using the DoEvents() method requires a reference to System.Windows.Forms.dll. Furthermore, the DoEvents() method is found in the class System.Windows.Forms.Application.
Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn1.Click
Dim intCounter as Integer
For intCounter = 1 To 1000
DoEvents() 'located in System.Windows.Forms.Application
'Processing Procedure here
Next
End Sub
DoEvents only works for Windows applications. Another option is to use the Thread.Sleep() method, under the System.Threading namespace. This can be used in all .Net projects.
ReplyDeleteI think this is actually closer to the old VB DoEvents method. I had read somewhere (the source escapes me at the moment) that the .Net DoEvents does not call Sleep method like was done in VB6 (and prior).