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
BackgroundWorker1.RunWorkerAsync()
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.