WithEvents specifies that one or more declared member variables refer to an instance of a class that can raise events.
Example: class with events
Public Class Export
Public Event TimesUp()
Public Sub New()
Dim worker As New BackgroundWorker()
AddHandler worker.DoWork, AddressOf DoWork
AddHandler worker.RunWorkerCompleted, AddressOf bg_RunWorkerCompleted
worker.RunWorkerAsync()
End Sub
Private Sub DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
' export stuff
Thread.Sleep(5000)
End Sub
Private Sub bg_RunWorkerCompleted(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
MsgBox("Background worker completed", vbInformation, "Background worker")
RaiseEvent TimesUp() 'Triggers an event declared at module level within a class, form, or document.
End Sub
End Class
Dim WithEvents _Export As New Export
Private Sub handleTimesUp() Handles _Export.TimesUpMsgBox("TimesUp Event Raised and Handled")
End Sub