title: VB.NET background task pass parameters and use return value in textfields
body: <p>I am just trying to figure out how to use the Background task, or something equiv, to pass in some parameters to the background task and then get return values from the tasks as they finish and update a text field...</p>
  
  <p>This is what I would like to do at a high level in VB.NET:</p>
  
  <ol>
  <li><p>User clicks button on form</p></li>
  <li><p>HTTP request performed in background thread with some args</p></li>
  <li><p>When response from request is received, update field on form with this data</p></li>
  </ol>
  
  <p>Here is the code:</p>
  
  <pre><code>    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
  </code></pre>