By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,899 Members | 1,230 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,899 IT Pros & Developers. It's quick & easy.

update a few components from a thread

RemusRigo
P: 2
I need a thread to populate a listview and to increase/update the values of two progress bars (in vb.net)

For better understanding, the form looks like this:



Can anyone help me out, I tried to use a backgroundworker, something like this:

Expand|Select|Wrap|Line Numbers
  1.    Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGO.Click
  2.       pbAction.Value = 0
  3.       bgwProcess.RunWorkerAsync()
  4.       Me.Cursor = Cursors.WaitCursor
  5.    End Sub
  6.  
  7.    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  8.       Application.Exit()
  9.    End Sub
  10.  
  11.    Private Sub bgwProcess_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwProcess.DoWork
  12.       'a job consists in retrieving data, populating a listview and update the progressbar
  13.       'do job 1.1 -> ProgressBar1.value+=1
  14.       'do job 1.2 -> ProgressBar1.value+=1
  15.       'do job 1.3 -> ProgressBar1.value+=1
  16.  
  17.       'start job 2.1 ProgressBar1.value=1
  18.       'do job 2.1 -> ProgressBar2.value+=1
  19.       'do job 2.2 -> ProgressBar1.value+=1
  20.  
  21.    End Sub
  22.  
  23.    Private Sub bgwProcess_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwProcess.RunWorkerCompleted
  24.       Me.Cursor = Cursors.Default
  25.    End Sub
Apr 22 '12 #1
Share this Question
Share on Google+
1 Reply


RemusRigo
P: 2
I have created a class that in the _doWork is populated, and i send it to the _ProgressChanged procedure, where I cand do whatever I want to the component on the form:

Expand|Select|Wrap|Line Numbers
  1. Public Class myObj
  2.       Public action As String
  3.       Public msg As String
  4.       Public pbAction As Integer
  5.       Public pbMsg As Integer
  6.    End Class
  7.  
  8. ...
  9.  
  10.    Private Sub btnGO_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGO.Click
  11.       bgwProcess.RunWorkerAsync()
  12.       Me.Cursor = Cursors.WaitCursor
  13.    End Sub
  14.  
  15.    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
  16.       Application.Exit()
  17.    End Sub
  18.  
  19.    Private Sub bgwProcess_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles bgwProcess.DoWork
  20.       Dim op As New myObj
  21.  
  22.       op.action = "my action"
  23.       op.msg = "My result: Done"
  24.       op.pbAction = 1
  25.       op.pbMsg = 1
  26.       bgwProcess.ReportProgress(0, op)
  27.  
  28.    End Sub
  29.  
  30.    Private Sub bgwProcess_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles bgwProcess.ProgressChanged
  31.       Dim obj As New myObj
  32.       obj = DirectCast(e.UserState, myObj)
  33.  
  34.       myListView.BeginUpdate()
  35.       Dim li As New ListViewItem(obj.action, 0)
  36.       li.SubItems.Add(obj.msg)
  37.  
  38.       myListView.Items.AddRange(New ListViewItem() {li})
  39.       myListView.EndUpdate()
  40.       myListView.EnsureVisible(myListView.Items.Count - 1)
  41.       myListView.Refresh()
  42.  
  43.       pbAction.Value = obj.pbAction
  44.       pbTotal.Value = obj.pbMsg
  45.    End Sub
  46.  
  47.    Private Sub bgwProcess_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles bgwProcess.RunWorkerCompleted
  48.       Me.Cursor = Cursors.Default
  49.    End Sub
Apr 23 '12 #2

Post your reply

Sign in to post your reply or Sign up for a free account.