ProgressBar -- MultiThreading

Posted on 2006-03-26
Last Modified: 2012-03-04
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?
Question by:VBMan_06
    LVL 85

    Accepted Solution

    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
    LVL 9

    Expert Comment

    by:Naveen Swamy

    Featured Post

    Why You Should Analyze Threat Actor TTPs

    After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

    Join & Write a Comment

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    Introduction As chip makers focus on adding processor cores over increasing clock speed, developers need to utilize the features of modern CPUs.  One of the ways we can do this is by implementing parallel algorithms in our software.   One recent…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    734 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    22 Experts available now in Live!

    Get 1:1 Help Now