dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: How to fix window freeze while copying files...

  1. #1
    Join Date
    Dec 2007
    Posts
    11

    How to fix window freeze while copying files...

    I have a small program that copies files from a few locations from a hd to another folder.... sometimes these files combined are in excess of 50gb.. I have the progress bar written and it works, except the window freezes while its copying files and then it suddenly will move the progress bar and freeze again... eventually it completes the copy but you dont really see a nice transition on the progress bar....

    Any help?

  2. #2
    Join Date
    Jan 2007
    Posts
    152

    Re: How to fix window freeze while copying files...

    is this in a program you have writen?
    if so, can you post some code for us to see?

  3. #3
    Join Date
    Dec 2007
    Posts
    11

    Re: How to fix window freeze while copying files...

    Code:
    Private dblTotal As Double
    Private strDocuments, strDesktop, strSize, strOutlook, strOE, strFav As String
    Private dblOutlookSize, dblDocumentsSize, dblOE, dblDesktop As Double
    
    Private Sub cmdBackup_Click()
    
    Dim fso As New FileSystemObject
    
    strOutlook = Environ("USERPROFILE") & "\Local Settings\Application Data\Microsoft\Outlook"
    strOE = Environ("USERPROFILE") & "\Local Settings\Application Data\Identities"
    strDocuments = Environ("USERPROFILE") & "\My Documents"
    strDesktop = Environ("USERPROFILE") & "\Desktop"
    strFav = Environ("USERPROFILE") & "\Favorites"
    
    If fso.FolderExists(strOutlook) = True Then
        dblTotal = dblTotal + fso.GetFolder(strOutlook).Size
        If Round((fso.GetFolder(strOutlook).Size / 1073741824), 2) > 0.99 Then
            lblOutlookSize.Caption = Round((fso.GetFolder(strOutlook).Size / 1073741824), 2) & " GB"
        ElseIf Round((fso.GetFolder(strOutlook).Size / 1048576), 2) > 0.99 Then
            lblOutlookSize.Caption = Round((fso.GetFolder(strOutlook).Size / 1048576), 2) & " MB"
        Else
            lblOutlookSize.Caption = Round((fso.GetFolder(strOutlook).Size / 1024), 2) & " KB"
        End If
    Else
        lblOutlookSize.Caption = "None"
    End If
    
    If fso.FolderExists(strOE) Then
        dblTotal = dblTotal + fso.GetFolder(strOE).Size
        If Round((fso.GetFolder(strOE).Size / 1073741824), 2) > 0.99 Then
            lblOEsize.Caption = Round((fso.GetFolder(strOE).Size / 1073741824), 2) & " GB"
        ElseIf (Round((fso.GetFolder(strOE).Size / 1048576), 2) > 0.99) Then
            lblOEsize.Caption = Round((fso.GetFolder(strOE).Size / 1048576), 2) & " MB"
        Else
            lblOEsize.Caption = Round((fso.GetFolder(strOE).Size / 1024), 2) & " KB"
        End If
    Else
        lblOEsize.Caption = "None"
    End If
    If fso.FolderExists(strDocuments) Then
        dblTotal = dblTotal + fso.GetFolder(strDocuments).Size
        If Round((fso.GetFolder(strDocuments).Size / 1073741824), 2) > 0.99 Then
            lblDocsize.Caption = Round((fso.GetFolder(strDocuments).Size / 1073741824), 2) & " GB"
        ElseIf (Round((fso.GetFolder(strDocuments).Size / 1048576), 2) > 0.99) Then
            lblDocsize.Caption = Round((fso.GetFolder(strDocuments).Size / 1048576), 2) & " MB"
        Else
            lblDocsize.Caption = Round((fso.GetFolder(strDocuments).Size / 1024), 2) & " KB"
        End If
    Else
        lblDocsize.Caption = "None"
    End If
    
    If fso.FolderExists(strDesktop) Then
        dblTotal = dblTotal + fso.GetFolder(strDesktop).Size
        If Round((fso.GetFolder(strDesktop).Size / 1073741824), 2) > 0.99 Then
            lblDesktopSize.Caption = Round((fso.GetFolder(strDesktop).Size / 1073741824), 2) & " GB"
        ElseIf (Round((fso.GetFolder(strDesktop).Size / 1048576), 2) > 0.99) Then
            lblDesktopSize.Caption = Round((fso.GetFolder(strDesktop).Size / 1048576), 2) & " MB"
        Else
           lblDesktopSize.Caption = Round((fso.GetFolder(strDesktop).Size / 1024), 2) & " KB"
        End If
    Else
        lblDesktopSize.Caption = "None"
    End If
    
    If fso.FolderExists(strFav) Then
        dblTotal = dblTotal + fso.GetFolder(strFav).Size
        If Round((fso.GetFolder(strFav).Size / 1073741824), 2) > 0.99 Then
            lblFavsize.Caption = Round((fso.GetFolder(strFav).Size / 1073741824), 2) & " GB"
        ElseIf (Round((fso.GetFolder(strFav).Size / 1048576), 2) > 0.99) Then
            lblFavsize.Caption = Round((fso.GetFolder(strFav).Size / 1048576), 2) & " MB"
        Else
            lblFavsize.Caption = Round((fso.GetFolder(strFav).Size / 1024), 2) & " KB"
        End If
    Else
        lblFavsize.Caption = "None"
    End If
    
    lblTotalSize.Caption = Round((dblTotal / 1073741824), 2) & " GB"
    
    
    userInput = txtLocation.Text
    
    
    If Not fso.FolderExists(userInput) Then
        fso.CreateFolder userInput
    End If
    ProgressBar1.Min = 0
    ProgressBar1.Max = 100
    ProgressBar1.Value = 0
    lblPercent.Caption = Int(ProgressBar1.Value * 100 / ProgressBar1.Max)
    lblPercent.Refresh
    
    If fso.FolderExists(strOutlook) Then
    fso.CopyFolder strOutlook, userInput & "\Outlook"
        DoEvents
        ProgressBar1.Value = (ProgressBar1.Value + CInt((fso.GetFolder(strOutlook).Size / dblTotal) * 100))
    End If
    
    If fso.FolderExists(strOE) Then
        fso.CopyFolder strOE, userInput & "\Outlook Express"
        DoEvents
        ProgressBar1.Value = (ProgressBar1.Value + CInt((fso.GetFolder(strOE).Size / dblTotal) * 100))
    End If
    
    If fso.FolderExists(strDocuments) Then
        fso.CopyFolder strDocuments, userInput & "\My Documents"
        DoEvents
        ProgressBar1.Value = (ProgressBar1.Value + CInt((fso.GetFolder(strDocuments).Size / dblTotal) * 100))
    End If
    
    If fso.FolderExists(strDesktop) Then
    fso.CopyFolder strDesktop, userInput & "\Desktop"
        DoEvents
        ProgressBar1.Value = ProgressBar1.Value + CInt((fso.GetFolder(strDesktop).Size / dblTotal) * 100)
    End If
    
    If fso.FolderExists(strFav) Then
    fso.CopyFolder strFav, userInput & "\Favorites"
        DoEvents
        ProgressBar1.Value = (ProgressBar1.Value + CInt((fso.GetFolder(strFav).Size / dblTotal) * 100))
    End If
    
        lblPercent.Caption = Int(ProgressBar1.Value * 100 / ProgressBar1.Max)
        lblPercent.Refresh
        
    
    End Sub
    I see already that i could just place the lblPrecent modifier/refresher at the end of each copy...

    I would try to fraise this in a loop but im not sure how i would do it.... Its been a while since i wrote any vb6 code.... Im trying ot learn vb.net so please forgive me if its obvious...

    But even if i did place it in a loop would it still freeze the window?

  4. #4
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to fix window freeze while copying files...

    CopyFolder doesn't indicate any progress. Your code will take forever to run.

    You should write this in .Net, using a couple of subthreads being controlled by a main thread.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Dec 2007
    Posts
    11

    Re: How to fix window freeze while copying files...

    Do you know any good compilers? I have little experience with .net so im looking for one that can help me autocomplete....

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to fix window freeze while copying files...

    Download Visual Studio 2005 Express and visit the VB.Net forum.

    VS 2008 is also available, but might be in beta still.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Dec 2007
    Posts
    11

    Re: How to fix window freeze while copying files...

    Wow VB 2008 Express looks awesome... downloading it now.... thanks for the info... ill convert my backup program to a vb.net application...

    Ill let you know how it turns out...

    Thanks,

    Heath

  8. #8
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How to fix window freeze while copying files...

    In VB 2005, I would have used a single statement My.Computer.FileSystem.CopyFile or My.Computer.FileSystem.CopyDirectory.
    It should be present in VB 2008 also.

  9. #9
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,264

    Re: How to fix window freeze while copying files...

    Quote Originally Posted by Shuja Ali
    It should be present in VB 2008 also.
    yes it is
    http://msdn2.microsoft.com/en-us/lib...5e(VS.90).aspx
    [SIGPIC][/SIGPIC]
    All my Articles
    Hannes

  10. #10
    Join Date
    Dec 2007
    Posts
    21

    Re: How to fix window freeze while copying files...

    Is it possible to use threads in VB?

  11. #11
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: How to fix window freeze while copying files...

    Not real easily in VB6, but VB.Net is made for multi-threading.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  12. #12
    Join Date
    Jun 2004
    Location
    Kashmir, India
    Posts
    6,808

    Re: How to fix window freeze while copying files...

    Quote Originally Posted by s00263668
    Is it possible to use threads in VB?
    This is not related to the topic discussed in this thread. Ideally you should have searched the forum and would have gotten your answer by now or you could have created a new thread.

    This might give you some insight why it is not easy to implement multi-threading and what are the workarounds.

  13. #13
    Join Date
    Dec 2007
    Posts
    21

    Re: How to fix window freeze while copying files...

    sorry about that

  14. #14
    Join Date
    Sep 2001
    Location
    Québec, Canada
    Posts
    1,923

    Re: How to fix window freeze while copying files...

    Just to let you know, it's possible to copy a file in VB6 without hanging the window, there is an API called CopyFileEx that is pretty easy to use, there is an example here:

    http://www.mentalis.org/apilist/CopyFileEx.shtml

    JeffB
    CodeGuru VB FAQ Visual Basic Frequently Asked Questions
    VB Code color Tool to color your VB code on CodeGuru
    Before you post Importants informations to know before posting

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)