Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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:

 

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.

  • 2 weeks later...
Posted

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:

 

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.

Posted

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

  • Administrators
Posted

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.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...