Garbage Collection of Threads

jccorner

Centurion
Joined
Jan 31, 2004
I've used threads in my apps before but wish to make sure that I'm implementing a thread correctly in this case.

Here is the function I call on a form.closed event:

Code:
Private Sub RefreshData
Dim ThrData As New Thread(AddressOf LoadMemos)
ThrData.Start()
End Sub

LoadMemos just simply reads a few tables from the database and fills a dataset that the forms will be using. My questions are how can I tell the thread is completed and how do I perform any clean up because every time any of the child forms (up to 5 forms) are closed a new thread is created and called again. Thank you for your time and appreciate your assistance.
 

jccorner

Centurion
Joined
Jan 31, 2004
Okay, well, can anyone answer this??

If I submit a thread as I did above and all it does is load six datasets as such:

Code:
Private Sub LoadMemos()
  Loadds(SqlString, DS1)
  Loadds(SqlString, DS2)
  Loadds(SqlString, DS3)
  Loadds(SqlString, DS4)
  Loadds(SqlString, DS5)
  Loadds(SqlString, DS6)
End Sub

Where Loadds is a function call that takes a sql string and the ds to fill but I want this filled in the background since it will be used throughout the app. After the last dataset if filled, what happens to the thread?? Is it still running?? Does it just end?? Is it taking up any memory?? Do I have to perform any cleanup?? Thank you for any help with this.
 

Diesel

Contributor
Joined
Aug 18, 2003
Visual Basic:
Dim threads As New ArrayList()

Private Sub RefreshData
Dim ThrData As New Thread(AddressOf LoadMemos)
threads.Add(ThrData)
ThrData.Start()
End Sub

 Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If

        For Each o As Object In threads
            If Not o Is Nothing Then
                Dim th As Thread = CType(o, Thread)
                If th.ThreadState = ThreadState.Background Or th.ThreadState = ThreadState.Running Then
                    th.Abort()
                    th = Nothing
                End If
            End If
        Next


        MyBase.Dispose(disposing)
    End Sub

Not sure if it's the best way to do it, but it works
 

PlausiblyDamp

Administrator
Joined
Sep 4, 2002
Location
Lancashire, UK
One way to tell if a thread is completed is to implement a callback method - tell the thread's method the address of another function to call when it has completed, this will avoid the need to do any polling etc.
Also if you do not need (or want) to keep track of the threads yourself you could look at using the ThreadPool class and let .Net handle the thread creation / destruction for you.
 

jccorner

Centurion
Joined
Jan 31, 2004
Everyone, thanks for the responses.

PD, I have changed my code to use the threadpooling, but I'm still not sure if that answers my question. Will the threadpooling destroy the thread after it has finished running??

For example, a new thread for LoadMemos is created and started each time a child form is called from the main form. When the child form closes, LoadMemos refills the dataset with the updated data. Now this could happen up to fifty times throughtout the day. After the thread has completed, will the threadpooling destroy the thread automatically?? Thanks again for the help.
 
Top Bottom