Ladies and Gentlemen,
your help please.
I want to create a background thread to calculate the size of a given directory. Since this process can be quite lenghty, I want to use a background thread. I'm using my D:\ here. I am using the following code in a test program that has one button, one textbox (txtDir.Text = "D:\") and a label (lblDirSize) to display the result. Unfortunately, I get two errors (Error 1 and Error 2 see below in orange) which I can’t seem to resolve.
VB Code:
[b]form1 code[/b] Dim WithEvents [COLOR=DarkOrange]BytesInDir[/COLOR] As DirSize 'Error 1 Private t As Thread Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim sourceDir As New DirectoryInfo(txtDir.Text) Dim dirBytes As New DirSize t = New Thread(AddressOf [COLOR=DarkOrange]dirBytes.CalcDirSizeInBytes(sourceDir)[/COLOR]) 'Error 2 t.Start() End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click t.Abort() End Sub Private Sub SizeCompleteEH(ByVal total As Long) Handles BytesInDir.SizeComplete lblDirSize.Text = "It worked! The total is: " & CStr(total) End Sub
Error 1 says 'WithEvents' variable does not raise any instance events that are accessible to 'Class Form1'.VB Code:
[b]Class code[/b] Public Class DirSize Public Shared Event SizeComplete(ByVal total As Long) Public Shared Function CalcDirSizeInBytes(ByVal dirInfo As System.IO.DirectoryInfo) As Long Dim total As Long = 0 Dim file As System.IO.FileInfo Dim dir As System.IO.DirectoryInfo Try For Each file In dirInfo.GetFiles() total += file.Length Next For Each dir In dirInfo.GetDirectories() total += CalcDirSizeInBytes(dir) Next RaiseEvent SizeComplete(total) Catch ex As Exception 'ignore errors for now End Try End Function End Class
Error 2 says 'AddressOf' operand must be the name of a method; no parentheses are needed.
Any ideas?
(If anyone knows of a better way to calculate a directory size then I would be interseted to know.)![]()




Reply With Quote