Simple threading question

vber

New member
Joined
Oct 4, 2005
Messages
3
Programming Experience
Beginner
Hi all,

I need to pass parameters to a method running in a thread. To do this I have created a global object with variables I wish to pass to the method, and a method in which im going to use for my thread function.

my question is: after the thread is complete, im left with a global object that I cannot delete and is eating up memory, so how can I delete this object? Can I delete it from itself, or create another thread to delete it, etc?

Thanks all!!
vber
 
How about this instead of a global variable:

PrivateSub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim t As Threading.Thread = New Threading.Thread(AddressOf dostuff)
t.Start()
EndSub

PrivateSub dostuff()
Dim obj AsObject = NewObject
doit(obj)
obj =
Nothing
EndSub

PrivateSub doit(ByVal o AsObject)
' Do some stuff
EndSub

 
hi mjb3030,

thanks for your reply, i could almost get away with that solution but i have this problem:

listening thread:
while 1
wait for data to arrive
create an object and store data in object
spawn a worker thread to manipulate data
end while

you see, if I don't use a global object then how could I pass it to the worker thread? And after worker thread completes, how could I delete the object?

Thanks!!
vber
 
Also,

when a thread completes its execution, does it automatically kill itself to free memory or do we have to manually kill it i.e. abort()?

Thanks!!
vber
 
Correct me if I'm wrong... it sounds like your object is local to the listening thread? It sounds like it should disappear when the listening thread is finished. Yes, when a thread is complete, it is killed and the memory is eventually freed.

If your object is global, then it will continue to use memory (as you are seeing). So, either declare it in your listening thread instead of in your global declarations or set

obj=nothing

right before the end of your listening thread.
 
Back
Top