Results 1 to 10 of 10

Thread: I can't get a background thread to work

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    83

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

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  3. #3
    Frenzied Member Asgorath's Avatar
    Join Date
    Sep 2004
    Location
    Saturn
    Posts
    2,036

    Re: I can't get a background thread to work

    VB Code:
    1. Public Event BytesInDir() As DirSize
    "The dark side clouds everything. Impossible to see the future is."

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    83

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

    VB Code:
    1. [b]Class[/b]
    2. Public Class DirSize
    3.     Public Shared Event SizeComplete(ByVal total As Long)
    4.  
    5.     Public Shared dirInfo As System.IO.DirectoryInfo
    6.     Public Shared total As Long
    7.  
    8.     Public Shared Function CalcDirSizeInBytes() As Long
    9.         Dim file As System.IO.FileInfo
    10.         Dim dir As System.IO.DirectoryInfo
    11.  
    12.         Try
    13.             For Each file In dirInfo.GetFiles()
    14.                 total += file.Length
    15.             Next
    16.  
    17.             For Each dir In dirInfo.GetDirectories()
    18.                 total += CalcDirSizeInBytes
    19.             Next
    20.  
    21.             RaiseEvent SizeComplete(total)
    22.         Catch ex As Exception
    23.             'ignore errors for now
    24.         End Try
    25.  
    26.     End Function
    27. 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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    83

    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?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    83

    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.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jul 2004
    Posts
    83

    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... :'(

  10. #10
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width