Results 1 to 6 of 6

Thread: [RESOLVED] BackgroundWorker Help

Threaded View

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2008
    Location
    Dominican Republic
    Posts
    733

    Resolved [RESOLVED] BackgroundWorker Help

    Hey guys .

    I have this app to copy files with a progressbar... What I'm trying to do is to set the progressbar1.maximum to the number of files in the folder.

    But I'm getting a cross-thread error.

    This is the error I'm getting:

    Cross-thread operation not valid: Control 'ProgressBar1' accessed from another thread than the thread it was created on.

    This is where I'm trying to set the ProgressBar1.Maximum:

    vb.net Code:
    1. Private Sub BackgroundWorker1_DoWork1 _
    2.     (ByVal sender As Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    3.  
    4.         Dim worker As System.ComponentModel.BackgroundWorker = DirectCast(sender, System.ComponentModel.BackgroundWorker)
    5.         ProgressBar1.Maximum = My.Computer.FileSystem.GetFiles("C:\From", FileIO.SearchOption.SearchAllSubDirectories, "*").Count
    6.         Dim i As Integer = 1
    7.         For Each file As String In My.Computer.FileSystem.GetFiles("C:\From", FileIO.SearchOption.SearchAllSubDirectories, "*")
    8.             My.Computer.FileSystem.CopyFile(file, String.Concat("E:\", Microsoft.VisualBasic.Right(file, Microsoft.VisualBasic.Len(file) - 3)), True)
    9.             worker.ReportProgress(i, i & " File(s) Copied")
    10.             i += 1
    11.         Next
    12.     End Sub

    I got this code from the F1 help of VS

    vb.net Code:
    1. ' Check if this method is running on a different thread
    2.     ' than the thread that created the control.
    3.     If Me.textBox1.InvokeRequired Then
    4.         ' It's on a different thread, so use Invoke.
    5.         Dim d As New SetTextCallback(AddressOf SetText)
    6.         Me.Invoke(d, New Object() {[NewText] + " (Invoke)"})
    7.     Else
    8.         ' It's on the same thread, no need for Invoke.
    9.         Me.textBox1.Text = [NewText] + " (No Invoke)"
    10.     End If

    How can I adjust it to my needs? What's d?

    Thanks in advance !
    Last edited by tassa; Jun 26th, 2009 at 05:21 PM.
    "In our profession, precision and perfection are not a dispensable luxury, but a simple necessity."
    Niklaus E. Wirth


    Rate any post that helped you, it's a good way of saying thanks
    Please specify your Visual Studio Version!

    Why rating is useful

    My Code Bank Submissions: How to determine Windows Version| Working With Mouse Events | Blocking Input Using API | Get host's IP | Minimize to system tray "animated" | Colored ListBox (custom fonts, colors, highlight) Updated -New Class! | [VS 2008] Strong encryption and hashing class - Updated! 31/August/2009 | Create a shortcut using IWshRuntimeLibrary

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width