Waiting for an Asynchronous Call with EndInvoke : Async « Thread « VB.Net Tutorial






Imports System
Imports System.Threading
Imports System.Runtime.InteropServices 

    Public Class AsyncDemo 
        Public Function TestMethod(ByVal callDuration As Integer,<Out> ByRef threadId As Integer) As String
            Console.WriteLine("Test method begins.")
            Thread.Sleep(callDuration)
            threadId = Thread.CurrentThread.ManagedThreadId()
            return String.Format("My call time was {0}.", callDuration.ToString())
        End Function
    End Class
    Public Delegate Function AsyncMethodCaller(ByVal callDuration As Integer,<Out> ByRef threadId As Integer) As String


    Public Class AsyncMain 
        Shared Sub Main() 

            Dim threadId As Integer
            Dim ad As New AsyncDemo()
            Dim caller As New AsyncMethodCaller(AddressOf ad.TestMethod)

            Dim result As IAsyncResult = caller.BeginInvoke(3000,threadId, Nothing, Nothing)

            Thread.Sleep(0)
            Console.WriteLine(Thread.CurrentThread.ManagedThreadId)

            Dim returnValue As String = caller.EndInvoke(threadId, result)

            Console.WriteLine("The call executed on thread {0}, with return value ""{1}"".", _
                threadId, returnValue)
        End Sub
    End Class








23.11.Async
23.11.1.Waiting for an Asynchronous Call with WaitHandle
23.11.2.Waiting for an Asynchronous Call with EndInvoke
23.11.3.Polling for Asynchronous Call Completion