VB.NET TaskCompleted Event for Asynchronous Method Call
Demonstrates a TaskCompleted event for an asynchronous Chilkat method call.
The event callback occurs in the background thread, and therefore any updates to the UI must
happen on the UI thread. For this reason, MethodInvoker is used to make updates to a TextBox..
Dim WithEvents http As New Chilkat.Http() Private Sub http_OnTaskCompleted(sender As Object, args As Chilkat.TaskCompletedEventArgs) Handles http.OnTaskCompleted Dim task As Chilkat.Task = args.Task ' This event callback is running in the background thread. ' To update a UI element, we must be on the UI thread.. Me.Invoke(New MethodInvoker( Sub() ' The task.UserData can be used to identify the particular asynchronous method call ' that this callback belongs to. If (task.UserData = "chilkatHomePage") Then ' An asychronous method is simply calling the corresponding synchronous method ' in a background thread. In this case, it is a call to QuickGetStr, ' which returned a string, or it returned Nothing for failure. This result is available ' via task.GetResultString. The LastErrorText for the background call ' is available in task.ResultErrorText Dim htmlPage As String = task.GetResultString() If (htmlPage Is Nothing) Then TextBox1.Text = task.ResultErrorText Else TextBox1.Text = htmlPage End If End If End Sub)) End Sub Private Sub AsyncToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles AsyncToolStripMenuItem.Click http = New Chilkat.Http() Dim task As Chilkat.Task = http.QuickGetStrAsync("http://www.chilkatsoft.com/") If (task Is Nothing) Then TextBox1.Text = http.LastErrorText Exit Sub End If ' We can set the task.UserData property to identify this particular asynchronous call in the callback. task.UserData = "chilkatHomePage" ' Start the task in a background thread Dim success As Boolean = task.Run() If (Not success) Then TextBox1.Text = task.LastErrorText Exit Sub End If End Sub
admin
0
Tags :