Click here to Skip to main content
15,615,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i tried the following.

i created the progress bar. and set the
visible property=false and

when i clicked the button1
change the progress bar visibility=true
and then do some lines...these lines are taking nearly about 2 mins.
after this line i will change the visible property=false.

but when i clicked the button1
its just shows the progress bar. then it hides.
after that intermediate lines are executed.

so my purpose of showing the progress bar is not happened.
please tell me why it happened like this.

What I have tried:

Public Sub button1_click()

        ProgressBar1.Visible = True

        Dim Size As Long = 0
        Dim fis As FileInfo() = d.GetFiles()
        Dim fi As FileInfo
        For Each fi In fis
            Size += fi.Length
        Next fi

        Dim dis As DirectoryInfo() = d.GetDirectories()
        Dim di As DirectoryInfo
        For Each di In dis
            Size += DirSize(di)
        Next di
        ProgressBar1.Visible = False
end sub
Updated 19-Jul-21 0:41am

That doesn't seem quite right, the only reasons I can think of that happening are:

  1. The logic executes really quickly
  2. The DirSize method runs on a separate thread

You need to provide more information on what the DirSize method is doing. If you're launching a separate thread to stop the UI from locking up, remember that the DirSize method will return before the thread has finished running, which means the progress bar will be hidden again.
Share this answer
Your code is running on the UI thread. Any updates you make to the UI will not be shown until your method returns.

You should move your code to a background thread using a BackgroundWorker[^]:
Public Sub button1_click() Handles button1.Click
    If Not BackgroundWorker1.IsBusy Then
        button1.Enabled = False
        ProgressBar1.Visible = True
    End If
End Sub

Private Sub CalculateDirectorySize(ByVal sender As Object, ByVal args As DoWorkEventArgs) Handles BackgroundWorker1.DoWork
    Dim Size As Long = 0
    Dim fis As IEnumerable(Of FileInfo) = d.EnumerateFiles()
    For Each fi As FileInfo In fis
        Size += fi.Length
    Next fi
    Dim dis As IEnumerable(Of DirectoryInfo) = d.EnumerateDirectories()
    For Each di As DirectoryInfo In dis
        Size += DirSize(di)
    Next di
End Sub

Private Sub CalculateDirectorySizeCompleted(ByVal sender As Object, ByVal args As RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted
    ProgressBar1.Visible = False
    button1.Enabled = True
End Sub
NB: You won't be able to update the properties of any controls from the CalculateDirectorySize method, since it's not running on the UI thread.
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900