VB.NET后台任务传递参数并在textfields中使用返回值

时间:2013-05-25 16:47:23

标签: vb.net

我只是想弄清楚如何使用Background任务或者其他东西来将一些参数传递给后台任务,然后在完成任务时从任务中获取返回值并更新文本字段... < / p>

这就是我想在VB.NET中高级做的事情:

  1. 用户点击表格上的按钮

  2. 在后台线程中使用一些args执行HTTP请求

  3. 收到来自请求的回复后,请使用此数据更新表单

  4. 以下是代码:

        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
    

0 个答案:

没有答案