PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197

PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
Textbox and progress bar not populating-VBForums
Results 1 to 4 of 4

Thread: Textbox and progress bar not populating

  1. #1

    Thread Starter
    Join Date
    May 2010

    Question Textbox and progress bar not populating

    I'm working on a tool to backup multiple folders to a network share. I had completed the first section (Documents) and it was working exactly how I needed it to. I then went on to favorites. It broke so I removed everything I did and went back to just documents.

    By "It broke" I mean, the textbox stopped visually populating (It's still obviously filling because the scroll bar shows something is going into the box, but I can't see anything) and the progress bar stopped filling.

    Once it completes the process, the text box and the progress bar populate. But they should be incrementally populating, not all at the end.

    txtActivity is the textbox
    prgBackup is the progress bar

    Here's the code I have.. Code:
    1. For Each foundFile As String In My.Computer.FileSystem.GetFiles("C:\users\" & thisUser & "\Documents")
    3.             ' Remove everything but the file name from the "foundFile" loop
    4.             Dim withParts As String = foundFile
    5.             Dim withoutParts As String = Replace(withParts, "C:\users\" & thisUser & "\Documents\", "")
    7.             ' Create the Documents directory on the network share
    8.             My.Computer.FileSystem.CreateDirectory(myDrive & "\Documents")
    10.             ' Copy each file to the network share
    11.             FileCopy(withParts, myDrive & "\Documents\" & withoutParts)
    13.             ' Update the Activity text box
    14.             txtActivity.Text = txtActivity.Text + vbCrLf + "Copying " & withoutParts
    15.             ' System.Threading.Thread.Sleep(1000)
    16.         Next
    17.         ' Increase Progress bar by 5
    18.         prgBackup.Value = prgBackup.Value + 5
    20.         txtActivity.Text = txtActivity.Text + vbCrLf + ("Backing up Favorites...")

    I REMd out my forced pauses as I thought they might have something to do with it but, it didn't seem to make a difference. If anyone can point me in the right direction, it'd be much appreciated.

    I also realized that it's not grabbing folders from "Documents" since I used "FileCopy". But when I try to use the following code... Code:
    1. My.Computer.FileSystem.CopyDirectory("C:\Users\" & thisUser & "\Documents", myDrive & "\Documents\", True)

    I get this error:

    An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
    Additional information: Access to the path 'C:\Users\dcj1978\Documents\My Music' is denied.
    Thanks in advance!
    Last edited by dcj1978; Mar 4th, 2015 at 12:15 PM.

  2. #2
    Hyperactive Member
    Join Date
    Mar 2012

    Re: Textbox and progress bar not populating

    As for why your controls seem to not do anything while the code is running is because controls are the last thing that a program will handle. It will post messages about the actions to complete to update the controls, but it won't actually do those actions until the end of the Sub. Often for applications like this, people will use a separate thread in order to do the file copying, and will have the main program / UI thread respond to events that the background thread issues to report on its progress. Here is an article in the Code Bank that discusses using multiple threads and here is a quick tutorial on using the Background Worker class in particular.

  3. #3

    Thread Starter
    Join Date
    May 2010

    Re: Textbox and progress bar not populating

    Perfect! Thanks so much.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002

    Re: Textbox and progress bar not populating

    In this case, threads might be overkill (though probably not). As Pyth007 noted, the controls appear not to do anything because the notices to paint the control are being posted to the message queue, but the UI is busy running your code, so it can't pump messages from the queue, so the painting never happens. The point to threading is that a background process handles the busy work while the UI remains free to deal with messages.

    If the ONLY issue is that the controls don't appear to be updating, you might consider just adding a YourTextbox.Refresh. That will cause the UI to stop and redraw the control immediately, rather than just posting a message and drawing when it gets around to it. Doing something like this will hurt performance, though only VERY slightly, because the painting will have to happen whenever the .Refresh is called. It's unlikely that you would see the performance difference, but if you are processing LOTS of things, then the total time taken will be a few seconds longer.

    However, by doing that, painting will be the ONLY thing that the UI will do. Button presses won't be recognized, and so on. So, the question kind of comes down to: Do you want the rest of the UI responsive while this is happening? You may not, in this example, because it might not be good for the user to be able to do anything else with the form while this is going on. If you DO want the form to be responsive, then use threading. If you just want the textbox to update, but don't want the rest of the form to respond to user actions, then just use the .Refresh method for any controls that are being changed.
    My usual boring signature: Nothing

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