VB.NETCreate Threads Without Creating Threads
Listing 8. Asynchronous delegates enable you to let the .NET Framework create worker threads for you automatically. You can monitor the state of the asynchronously executing method as well by using the state data of the IAsyncResult interface. ![]() ' Create a delegate that will ' be called asynchronously Private Delegate Function GetTextData _ (ByVal DatabaseName As String, _ ByVal ProcName As String) As String Dim async As New GetTextData _ (AddressOf TextProxy) Dim asyncResult As IAsyncResult Sub LoadTextData _ (ByVal DatabaseName As String, _ ByVal ProcName As String) ' Check to see what thread this ' code is executing on Console.WriteLine _ ("Async Delegate called on: " & _ Thread.CurrentThread.Name) ' Call BeginInvoke on the TextProxy ' method asynchronously, this ' will fire the delegate's method asyncResult = async.BeginInvoke _ (DatabaseName, ProcName, _ Nothing, Nothing) End Sub Function TextProxy _ (ByVal DatabaseName As String, _ ByVal ProcName As String) As String Console.WriteLine _ ("Async Delegate running on: " & _ Thread.CurrentThread.Name) lblTextName.Text = _ "Database: " & DatabaseName & _ Space(15) & "Object: " & ProcName ' Show the results in the Textbox Me.TextDisplay.Text = _ _dbHelper.GetStoredProcedureText _ (DatabaseName, ProcName) End Function |