Как правильно использовать CancelEventAgrs / CancelEventHandler?
Проверяет ли e.Cancel
всегда ли верный результат, даже если событие со временем может занять некоторое время?
Class Foo
Public Event Deleting As System.ComponentModel.CancelEventHandler
Private myObjectsToDelete As List(Of Object)
Function DoDelete() As Boolean
Dim e As New System.ComponentModel.CancelEventArgs
' Suppose a time consumming check '
RaiseEvent Deleting(myObjectsToDelete, e)
If e.Cancel Then
Return False
Else
myObjectsToDelete.Clear()
Return True
End If
End Function
End Class
Class Bar
Private WithEvents myFoo As Foo
Private Sub myFoo_Deleting( _
sender As Object,
e As System.ComponentModel.CancelEventArgs) Handles myFoo.Deleting
Dim objectsList As List(Of Object) = TryCast(sender, List(Of Object))
Dim objectsToDeleteOK As New List(Of Object)
For Each o In objectsList
If CheckIfCanBeDeleted(o) Then
objectsToDeleteOK.Add(o)
End If
Next o
End Sub
Private Function CheckIfCanBeDeleted(o As Object) As Boolean
Dim result As Boolean
' TIME CONSUMMING CHECK '
Return result
End Function
End Class
PS.
Я поставил тег C #, потому что заинтересован в общем представлении .NET, а не только в VB.NET