|
-
Sep 27th, 2005, 07:23 AM
#1
Thread Starter
Lively Member
I can't get a background thread to work
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
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 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.)
-
Sep 27th, 2005, 07:54 AM
#2
Re: I can't get a background thread to work
1. You have nominated an instance of the class when you really don't need to. Use "Handles DirSize.SizeComplete" instead and get rid of the entire line that says "withevents..."
2. You cannot pass arguments to a method when you are using addressof. You need to get the data in there using different means (maybe a nearby global variable).
Last edited by wossname; Sep 27th, 2005 at 08:00 AM.
I don't live here any more.
-
Sep 27th, 2005, 08:02 AM
#3
Re: I can't get a background thread to work
VB Code:
Public Event BytesInDir() As DirSize
"The dark side clouds everything. Impossible to see the future is."
-
Sep 27th, 2005, 08:41 AM
#4
Thread Starter
Lively Member
Re: I can't get a background thread to work
Asgorath and wossname,
Thanks for you replies.
I have altered the code slightly but I still get errors. here is my attempt:
VB Code:
[b]Form1[/b]
Private t As Thread
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dirBytes As New DirSize
dirBytes.dirInfo = New DirectoryInfo(txtDir.Text)
t = New Thread(AddressOf [b]dirBytes.CalcDirSizeInBytes[/b]) 'Error 1
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 [b]DirSize[/b].SizeComplete ' Error 2
lblDirSize.Text = "It worked! The total is: " & CStr(total)
End Sub
VB Code:
[b]Class[/b]
Public Class DirSize
Public Shared Event SizeComplete(ByVal total As Long)
Public Shared dirInfo As System.IO.DirectoryInfo
Public Shared total As Long
Public Shared Function CalcDirSizeInBytes() As Long
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
Next
RaiseEvent SizeComplete(total)
Catch ex As Exception
'ignore errors for now
End Try
End Function
End Class
Error 1: Method 'Public Shared Function CalcDirSizeInBytes() As Long' does not have the same signature as delegate 'Delegate Sub ThreadStart()'.
Error 2: Handles clause requires a WithEvents variable.
Error 2 is confusing slightly, since I have got rid of the WithEvents line!
Asgorath where should I put the 'Public Event BytesInDir() As DirSize' line?
I tried at the top of Form 1 code but it didn't like it.
-
Sep 27th, 2005, 09:10 AM
#5
Re: I can't get a background thread to work
1. The method that you use as the entry point for a new thread must take no arguments and return no value.
2. Your Handles clause must be for an event of a specific variable that has been declared WithEvents. DirSize is a type, not a variable. You must specify a specific DirSize reference (variable) in the Handles clause. If you are creating your DirSize object as a local variable in a method, you will need to use the AddHandler statement as you can only use a Handles clause for class-level variables.
-
Sep 27th, 2005, 09:33 AM
#6
Thread Starter
Lively Member
Re: I can't get a background thread to work
Ok, I've added
Dim WithEvents BytesInDir As DirSize at the top of the program and cleaned up the entry point as you suggested.
But unfortunately I get an error with the above line which reads:
'WithEvents' variable does not raise any instance events that are accessible to 'Class Form1'.
I don't understand this one... "accessible to Form1" Hhmmm.
What have i done wrong here?
-
Sep 27th, 2005, 09:42 AM
#7
Re: I can't get a background thread to work
Oops. I didn't notice that your event was Shared before. I've never tried to use a Shared event before. According to the error messages you are getting, the Handles clause is valid only for instance events of a specific variable. Why have you declared all the members of the DirSize class as Shared and then you go and create an instance of it. That doesn't make any sense. There is no point creating an instance of a class that has no instance members. Is there a specific reason you have declared all the members as Shared.
-
Sep 27th, 2005, 09:46 AM
#8
Thread Starter
Lively Member
Re: I can't get a background thread to work
Mainly because it is a recursive function that gets the total size in bytes. It was a legacy that I tried to get working yesterday. Unfortunately, that has also resulted in other issues.
-
Sep 27th, 2005, 09:49 AM
#9
Thread Starter
Lively Member
Re: I can't get a background thread to work
Ok I'll have a re-tink on this one - again... 
now I've just got to convince the boss... :'(
-
Sep 27th, 2005, 09:55 AM
#10
Re: I can't get a background thread to work
The fact that it's recursive makes no difference to whether it should be Shared or not. Make all your members instance members and the issue should go away. You're treating them like instance members anyway.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|