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:
  1. [b]form1 code[/b]
  2. Dim WithEvents [COLOR=DarkOrange]BytesInDir[/COLOR] As DirSize 'Error 1
  3. Private t As Thread
  4.  
  5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  6.         Dim sourceDir As New DirectoryInfo(txtDir.Text)
  7.         Dim dirBytes As New DirSize
  8.  
  9.         t = New Thread(AddressOf [COLOR=DarkOrange]dirBytes.CalcDirSizeInBytes(sourceDir)[/COLOR]) 'Error 2
  10.         t.Start()
  11.     End Sub
  12.  
  13.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
  14.         t.Abort()
  15.     End Sub
  16.  
  17.     Private Sub SizeCompleteEH(ByVal total As Long) Handles BytesInDir.SizeComplete
  18.         lblDirSize.Text = "It worked! The total is: " & CStr(total)
  19.     End Sub

VB Code:
  1. [b]Class code[/b]
  2. Public Class DirSize
  3.     Public Shared Event SizeComplete(ByVal total As Long)
  4.  
  5.     Public Shared Function CalcDirSizeInBytes(ByVal dirInfo As System.IO.DirectoryInfo) As Long
  6.         Dim total As Long = 0
  7.         Dim file As System.IO.FileInfo
  8.         Dim dir As System.IO.DirectoryInfo
  9.  
  10.         Try
  11.             For Each file In dirInfo.GetFiles()
  12.                 total += file.Length
  13.             Next
  14.  
  15.             For Each dir In dirInfo.GetDirectories()
  16.                 total += CalcDirSizeInBytes(dir)
  17.             Next
  18.  
  19.             RaiseEvent SizeComplete(total)
  20.         Catch ex As Exception
  21.             'ignore errors for now
  22.         End Try
  23.  
  24.     End Function
  25. End Class
Error 1 says 'WithEvents' variable does not raise any instance events that are accessible to 'Class Form1'.


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.)