Results 1 to 6 of 6

Thread: [RESOLVED] BackgroundWorker Help

  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

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: BackgroundWorker Help

    Create a delegate type for your needs, I'll call it OneArgD. Put it after the "Class/Module name" statement in your file.
    vb.net Code:
    1. Delegate Sub OneArgD(ByVal arg As Object)
    Next, you need to use Me.ProgressBar1.Invoke.
    Inside your backgroundWorker event handler, where you want to set the Maximum property:
    vb.net Code:
    1. Dim deleg As New OneArgD(AddressOf SetMaximum)
    2. Me.ProgressBar1.Invoke(deleg,My.Computer.FileSystem.GetFiles("C:\From", FileIO.SearchOption.SearchAllSubDirectories, "*").Count)

    And finally, add a Private Sub declaration to your class/module...
    vb.net Code:
    1. Private Sub SetMaximum(ByVal max As Object)
    2. Me.ProgressBar1.Maximum = CInt(max)
    3. End Sub

    I know it's really annoying to do that, but it's the only way.
    Last edited by minitech; Jun 26th, 2009 at 05:34 PM.

  3. #3

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

    Re: BackgroundWorker Help

    Can you give me an example please? (I'm searching MSDN for an example too, just though yours might be clearer)
    "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

  4. #4
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: BackgroundWorker Help

    Sorry, I'm editing it progressively, just keep clicking Refresh to see updates.

  5. #5
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: BackgroundWorker Help

    Ok, done. See post #2.

  6. #6

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

    Re: BackgroundWorker Help

    Thanks a lot!
    "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