Results 1 to 21 of 21

Thread: [2008] Copying big files

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    [2008] Copying big files

    I have a code that copies a folder of files to different directory and sorts them and they are about 700mb but the problem im having is that when it runs on form load the form freezes and doesnt refresh till its done so my question is what can i use that can handle this better?
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  2. #2
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] Copying big files

    Use a background worker to do the copy of the files.

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Copying big files

    will i still be able to show the status of how things are going? Also how do i copy using background worker? Ive been searching google and cannot find anything to help me out.
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  4. #4
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] Copying big files

    The background worker is a component you can add to a form from the designer. The background worker also has a ProgressChanged event, which you can fire and then in that event handler you can update a progress bar on the main form, for example:

    Code:
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            BackgroundWorker1.RunWorkerAsync()
        End Sub
    
    
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            For i As Integer = 0 To 20
                System.Threading.Thread.Sleep(500)
                BackgroundWorker1.ReportProgress(i * 5)
            Next
        End Sub
    
        Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As System.Object, ByVal e As System.ComponentModel.ProgressChangedEventArgs) Handles BackgroundWorker1.ProgressChanged
            ProgressBar1.Value = e.ProgressPercentage
        End Sub

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Copying big files

    Ok i understand that much but do i use the same code as i would in a regular form to copy?

    Edit also i tried debugging as is and i get this error:
    "This BackgroundWorker states that it doesn't report progress. Modify WorkerReportsProgress to state that it does report progress."
    Last edited by youngbucks; Jul 17th, 2008 at 07:44 PM.
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] Copying big files

    Yep, just put your copying code into the DoWork function.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Copying big files

    Ok thanks but one more thing, i keep getting this error:
    ""This BackgroundWorker states that it doesn't report progress. Modify WorkerReportsProgress to state that it does report progress.""
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  8. #8
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] Copying big files

    The error message is telling you exactly what you need to do. Modify the WorkerReportsProgress property.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Copying big files

    I realized and i fixed it, you replied too fast lol.. Thanks for the help.
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Copying big files

    Next problem just came up. "Cross-thread operation not valid: Control 'txtInstallLog' accessed from a thread other than the thread it was created on."

    and its reffering too the part of my code where i have:
    vb Code:
    1. Private Sub InstallLog(ByVal strLog As String)
    2.         txtInstallLog.Text = txtInstallLog.Text & strLog & vbCrLf
    3.         txtInstallLog.SelectionStart = txtInstallLog.Text.Length
    4.     End Sub

    How do i allow Cross-thread/fix this?
    Attached Images Attached Images  
    Last edited by youngbucks; Jul 17th, 2008 at 08:06 PM. Reason: Added screenshot
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  11. #11
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] Copying big files

    You will need to use a delegate if you want to access form controls from your background worker:

    http://www.vbforums.com/showthread.p...light=delegate

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Copying big files

    I dont understand 100% on what to do here as its throwing an exception to the labels and textbox i have on the form and since its in Backgroundworker1 im confused at to what goes in Addressof or what to put as string(I know what must be done i just dont know how to do it). Please bare with me as im totally new to using background worker.
    Last edited by youngbucks; Jul 17th, 2008 at 08:29 PM.
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  13. #13
    Frenzied Member MaximilianMayrhofer's Avatar
    Join Date
    Aug 2007
    Location
    IM IN YR LOOP
    Posts
    2,001

    Re: [2008] Copying big files

    Here is a link that will help you immensely: Accessing Controls from Worker Threads

  14. #14

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Copying big files

    I had a look at that also but what would i put here "(AddressOf ResetTextBoxText)" . My status(label) is supposed to change the text multiple times throughout and display something else so how do i end up setting that up?
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Copying big files

    Ive been doing that for every part that it throws the exception but now ive came to a stop:
    vb Code:
    1. Private Sub InstallLog(ByVal strLog As String)
    2.         If Me.txtInstallLog.InvokeRequired Then
    3.             Me.txtInstallLog.Invoke(New MethodInvoker(AddressOf InstallLog))
    4.         Else
    5.             Try
    6.                 txtInstallLog.Text = txtInstallLog.Text & strLog & vbCrLf
    7.                 txtInstallLog.SelectionStart = txtInstallLog.Text.Length
    8.             Catch ex As Exception
    9.                 MsgBox(ex.ToString)
    10.             End Try
    11.         End If
    12.     End Sub

    The error i get is "Method 'Private Sub InstallLog(strLog As String)' does not have a signature compatible with delegate 'Delegate Sub MethodInvoker()'.
    " and it give me the error in this line "Me.txtInstallLog.Invoke(New MethodInvoker(AddressOf InstallLog))"
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  16. #16
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] Copying big files

    Did you read the article we posted? It goes over the exact scenario if you have to pass parameters to the fucntion call. It's the second post. Basically you need to create your own delegate type to handle the scenario:

    Code:
        Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
            For i As Integer = 0 To 20
                System.Threading.Thread.Sleep(500)
                BackgroundWorker1.ReportProgress(i * 5)
                TextBox1.Invoke(New UpdateTextInvoker(AddressOf UpdateText), i * 5 & " % Complete")
            Next
        End Sub
    
        Private Delegate Sub UpdateTextInvoker(ByVal Value As String)
        Private Sub UpdateText(ByVal Value As String)
            TextBox1.Text &= Value & Environment.NewLine
        End Sub

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Copying big files

    I did. This is what i have (I for got to add the delegate part ot the post above):

    vb Code:
    1. Private Delegate Sub SetTextInvoker(ByVal strLog As String)
    2.     Private Sub InstallLog(ByVal strLog As String)
    3.         Try
    4.             txtInstallLog.Text = txtInstallLog.Text & strLog & vbCrLf
    5.             txtInstallLog.SelectionStart = txtInstallLog.Text.Length
    6.         Catch ex As Exception
    7.             MsgBox(ex.ToString)
    8.         End Try
    9.  
    10.  Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    11.         Me.txtInstallLog.Invoke(New MethodInvoker(AddressOf InstallLog))

    What am i missing because i obviously am.
    Last edited by youngbucks; Jul 18th, 2008 at 11:00 PM.
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  18. #18

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Copying big files

    I got it to stop showing the error but now i get "Parameter count mismatch."
    vb Code:
    1. Private Delegate Sub InstallLogInvoker(ByVal strLog As String)
    2.     Private Sub InstallLog(ByVal strLog As String)
    3.         Try
    4.             txtInstallLog.Text = txtInstallLog.Text & strLog & vbCrLf
    5.             txtInstallLog.SelectionStart = txtInstallLog.Text.Length
    6.         Catch ex As Exception
    7.             MsgBox(ex.ToString)
    8.         End Try
    9.     End Sub
    10.  
    11. Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    12.         Me.txtInstallLog.Invoke(New InstallLogInvoker(AddressOf InstallLog))
    13. End Sub
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  19. #19
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: [2008] Copying big files

    You are getting a parameter count mismatch, because you are not sending a parameter for strLog.

    Code:
    Me.txtInstallLog.Invoke(New InstallLogInvoker(AddressOf InstallLog),"Write this to the log.")

  20. #20

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Copying big files

    So everytime its supposed say something i have to invoke it?
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Jun 2008
    Location
    Nowhere
    Posts
    427

    Re: [2008] Copying big files

    I fixed that problem by doing this
    vb Code:
    1. Me.txtInstallLog.Invoke(New InstallLogInvoker(AddressOf InstallLog), "Currently installing, please wait.")

    But how do i fix the one that shows status in my label? Its just a simple Status.Text = ("Installation Started.") and doesnt have its own sub.
    "Programming is like sex. One mistake and you have to support it for the rest of your life." ~Michael Sinz


    Code Snippets/Usefull Links:
    WinRAR DLL|Vista Style Form|Krypton Component Library|Windows Form Aero|Parsing XML files|Calculate File's CRC 32|Download a list of files.

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