• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1042
  • Last Modified:

ProgressBar -- MultiThreading

I have an app that allows the user to click a button which starts a data migration process.  The migration process takes a really long time.  While it runs I need to show the user the status - progressbar, recs processed.  What is the best way to do this? The only way I know to run this process without the form hanging (not responding) is to run it in a seperate thread.  But then I have to talk back to the form to update the progress bar or lablels.  should i start the process with thread.start(proc) and then raiseevents from the process that are handled in the form for updating the controls?
1 Solution
Mike TomlinsonMiddle School Assistant TeacherCommented:
You have to use a Delegate and the Invoke method to marshal the call onto the controls thread if necessary.

Here is a simple example...

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim t As New System.Threading.Thread(AddressOf Me.NewThread)
    End Sub

    Private Delegate Sub UpdateLabelDelegate(ByVal value As String)

    Private Sub NewThread()
        Dim i As Integer
        For i = 1 to 5
            UpdateLabel("Test" & i)
        Next i
    End Sub

    Private Sub UpdateLabel(ByVal value As String)
        If Label1.InvokeRequired Then
            Dim uld As New UpdateLableDelegate(AddressOf UpdateLabel)
            Label1.Invoke(uld, New Object() {value})
            Label1.Text = value
        End If
    End Sub

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now