title: How to pass text parameter to ProgressChanged?
body: <p>I'm a VB.Net newbie, and need to write a long-running process with a While/End While loop.</p>
  
  <p>To avoid freezing the UI, I added a BackgroundWorker object on the form.</p>
  
  <p>Next, I needed to update the UI, but found that the thread cannot do this. Instead, the thread must call ReportProgress() to trigger the ProgressChanged() event.</p>
  
  <p>However, I need to pass the text from the exception (ex.Message) to the event, but didn't find an example on how to do this. I need that text message to update the form's title bar.</p>
  
  <p>Here's the code:</p>
  
  <pre><code>Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
      While (True)
          Try
              ...
          Catch ex As Exception
              'Cross-thread operation not valid: Control 'Form1' accessed from a thread other than the thread it was created on.
              'Me.Text = ex.Message
  
              BackgroundWorker1.ReportProgress(100)
          End Try
  
          System.Threading.Thread.Sleep(2000)
      End While
  End Sub
  
  Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
  
          'How to get ex.Message, and change the form's title bar accordingly?
          'Me.Text = ???
  End Sub
  </code></pre>
  
  <p>Thank you.</p>
  
  <hr>
  
  <p>Edit: Here's how to get the pass the error message to the event, and change the form's title text:</p>
  
  <pre><code>        Catch ex As Exception
              BackgroundWorker1.ReportProgress(100, ex.Message)
          End Try
  
          System.Threading.Thread.Sleep(2000)
      End While
  End Sub
  
  Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
      Me.Text = e.UserState.ToString
  End Sub
  </code></pre>