Vb.net 使用线程访问更多组件

Vb.net 使用线程访问更多组件,vb.net,multithreading,backgroundworker,Vb.net,Multithreading,Backgroundworker,我正在尝试使用线程访问一些组件。我的表单如下所示: Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGO.Click pbAction.Value = 0 bgwProcess.RunWorkerAsync() Me.Cursor = Cursors.WaitCursor End Sub Privat

我正在尝试使用线程访问一些组件。我的表单如下所示:

   Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGO.Click
      pbAction.Value = 0
      bgwProcess.RunWorkerAsync()
      Me.Cursor = Cursors.WaitCursor
   End Sub

   Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
      Application.Exit()
   End Sub

   Private Sub bgwProcess_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwProcess.DoWork
      'a job consists in retrieving data, populating a listview and update the progressbar
      'start job 1.1
      'do job 1.1 -> ProgressBar1.value+=1
      'do job 1.2 -> ProgressBar1.value+=1
      'do job 1.3 -> ProgressBar1.value+=1

      'start job 2.1 ProgressBar1.value=1
      'do job 2.1 -> ProgressBar2.value+=1
      'do job 2.2 -> ProgressBar1.value+=1

   End Sub

   Private Sub bgwProcess_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwProcess.RunWorkerCompleted
      Me.Cursor = Cursors.Default
   End Sub

我的消息来源如下:

   Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGO.Click
      pbAction.Value = 0
      bgwProcess.RunWorkerAsync()
      Me.Cursor = Cursors.WaitCursor
   End Sub

   Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
      Application.Exit()
   End Sub

   Private Sub bgwProcess_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwProcess.DoWork
      'a job consists in retrieving data, populating a listview and update the progressbar
      'start job 1.1
      'do job 1.1 -> ProgressBar1.value+=1
      'do job 1.2 -> ProgressBar1.value+=1
      'do job 1.3 -> ProgressBar1.value+=1

      'start job 2.1 ProgressBar1.value=1
      'do job 2.1 -> ProgressBar2.value+=1
      'do job 2.2 -> ProgressBar1.value+=1

   End Sub

   Private Sub bgwProcess_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwProcess.RunWorkerCompleted
      Me.Cursor = Cursors.Default
   End Sub

有人能帮我吗?

我创建了一个类,该类在“工作”中填充,并将其发送到“进程更改”过程,在该过程中,我可以对表单上的组件执行任何操作:

Public Class myObj
      Public action As String
      Public msg As String
      Public pbAction As Integer
      Public pbMsg As Integer
   End Class

...

   Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGO.Click
      bgwProcess.RunWorkerAsync()
      Me.Cursor = Cursors.WaitCursor
   End Sub

   Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
      Application.Exit()
   End Sub

   Private Sub bgwProcess_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwProcess.DoWork
      Dim op As New myObj

      op.action = "my action"
      op.msg = "My result: Done"
      op.pbAction = 1
      op.pbMsg = 1
      bgwProcess.ReportProgress(0, op)

   End Sub

   Private Sub bgwProcess_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgwProcess.ProgressChanged
      Dim obj As New myObj
      obj = DirectCast(e.UserState, myObj)

      myListView.BeginUpdate()
      Dim li As New ListViewItem(obj.action, 0)
      li.SubItems.Add(obj.msg)

      myListView.Items.AddRange(New ListViewItem() {li})
      myListView.EndUpdate()
      myListView.EnsureVisible(myListView.Items.Count - 1)
      myListView.Refresh()

      pbAction.Value = obj.pbAction
      pbTotal.Value = obj.pbMsg
   End Sub

   Private Sub bgwProcess_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwProcess.RunWorkerCompleted
      Me.Cursor = Cursors.Default
   End Sub

这是你问题的答案吗?这对我来说很有效,但是如果有人有其他想法,请告诉我