我只是想弄清楚如何使用Background任务或者其他东西来将一些参数传递给后台任务,然后在完成任务时从任务中获取返回值并更新文本字段... < / p>
这就是我想在VB.NET中高级做的事情:
用户点击表格上的按钮
在后台线程中使用一些args执行HTTP请求
收到来自请求的回复后,请使用此数据更新表单
以下是代码:
Public Class Form1
Dim WebUpdateWorker As BackgroundWorker
Private Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
Dim arg1 As String = "Hi"
Dim arg2 As Integer = 1
WebUpdateWorker = New BackgroundWorker
AddHandler WebUpdateWorker.DoWork, AddressOf DoWork
AddHandler WebUpdateWorker.RunWorkerCompleted, AddressOf WorkFinished
WebUpdateWorker.RunWorkerAsync()
End Sub
Public Function httpTask(ByVal arg1 As String, ByVal arg2 As Integer)
' do http request, return the http response
Return "http response"
End Function
Private Sub DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
' I want to pass these values in instead of hard coding them here
httpTask("Hi", 1)
End Sub
Private Sub WorkFinished(ByVal sender As Object, ByVal e As RunWorkerCompletedEventArgs)
' I want to use the returned value from httpTask here
TextBox.Text = ("Result from background job: ")
End Sub
End Class