include::../why-dotnet.adoc[] == How to fix it === Code examples ==== Noncompliant code example `BeginInvoke` without callback: [source,vbnet,diff-id=1,diff-type=noncompliant] ---- Public Delegate Function AsyncMethodCaller() As String Public Class Sample Public Sub DoSomething() Dim Example As New AsyncExample() Dim Caller As New AsyncMethodCaller(Example.SomeMethod) ' Initiate the asynchronous call. Dim Result As IAsyncResult = Caller.BeginInvoke(Nothing, Nothing) ' Noncompliant: Not paired With EndInvoke End Sub End Class ---- `BeginInvoke` with callback: [source,vbnet,diff-id=2,diff-type=noncompliant] ---- Public Delegate Function AsyncMethodCaller() As String Public Class Sample Public Sub DoSomething() Dim Example As New AsyncExample() Dim Caller As New AsyncMethodCaller(Example.SomeMethod) ' Initiate the asynchronous call. Dim Result As IAsyncResult = Caller.BeginInvoke(New AsyncCallback(Sub(ar) End Sub), Nothing) ' Noncompliant: Not paired With EndInvoke End Sub End Class ---- ==== Compliant solution `BeginInvoke` without callback: [source,vbnet,diff-id=1,diff-type=compliant] ---- Public Delegate Function AsyncMethodCaller() As String Public Class Sample Public Function DoSomething() As String Dim Example As New AsyncExample() Dim Caller As New AsyncMethodCaller(Example.SomeMethod) ' Initiate the asynchronous call. Dim Result As IAsyncResult = Caller.BeginInvoke(Nothing, Nothing) ' ... Return Caller.EndInvoke(Result) End Function End Class ---- `BeginInvoke` with callback: [source,vbnet,diff-id=2,diff-type=compliant] ---- Public Delegate Function AsyncMethodCaller() As String Public Class Sample Public Sub DoSomething() Dim Example As New AsyncExample() Dim Caller As New AsyncMethodCaller(Example.SomeMethod) ' Initiate the asynchronous call. Dim Result As IAsyncResult = Caller.BeginInvoke(New AsyncCallback(Sub(ar) ' Call EndInvoke to retrieve the results. Dim Ret As String = Caller.EndInvoke(ar) ' ... End Sub), Nothing) End Sub End Class ---- include::../resources-dotnet.adoc[] include::../rspecator.adoc[]