jccorner Posted November 28, 2005 Posted November 28, 2005 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. Quote Applying computer technology is simply finding the right wrench to pound in the correct screw
jccorner Posted December 7, 2005 Author Posted December 7, 2005 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. Quote Applying computer technology is simply finding the right wrench to pound in the correct screw
Leaders snarfblam Posted December 7, 2005 Leaders Posted December 7, 2005 How long does it take for a call to LoadMemos? Quote [sIGPIC]e[/sIGPIC]
Diesel Posted December 7, 2005 Posted December 7, 2005 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 Quote
Administrators PlausiblyDamp Posted December 9, 2005 Administrators Posted December 9, 2005 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. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
jccorner Posted December 12, 2005 Author Posted December 12, 2005 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. Quote Applying computer technology is simply finding the right wrench to pound in the correct screw
Administrators PlausiblyDamp Posted December 12, 2005 Administrators Posted December 12, 2005 That is pretty much exactly what the thread pool is there for. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.