public sub DoWork()
' do work
Thread.CurrentThread.Abort()
end sub
end class
In this class I start the threads
public class ThreadStarter
public sub StartMyThreads()
if threadcnt < maxThreadCnt then
dim tWorker as new MyThreadWorkerClass
dim t as new Thread(AddressOf MyThreadWorkerClass.DoWork)
t.Start()
threadcnt += 1
end if
end sub
end class
My question is this. When the thread aborts itself, does that kill the thread and tworker object (set them to nothing) or does this leave objects hanging, eventually leading to a memory leak?
How would you set the objects to nothing otherwise when the thread finishes?
Mike TomlinsonMiddle School Assistant TeacherCommented:
It kills the thread...though instead of Abort() you could just use "Exit Sub" as the thread dies when the end of the method is reached.
If there are no other references to your MyThreadWorkerClass instances then they will be marked for garbage collection and eventually destroyed automatically.
Also, instead of:
dim t as new Thread(AddressOf MyThreadWorkerClass.DoWork)
It would be:
dim t as new Thread(AddressOf tWorker.DoWork)
*Though you probably already knew that and this is just example code.
0
rutledgjAuthor Commented:
So does the new t as Thread also get marked for GC? Are both objects marked for gc when the thread completes?
Mike TomlinsonMiddle School Assistant TeacherCommented:
Correct.
Since it is a local variable and goes out of scope after StartMyThreads() exits you will have no way of referencing it. As such, it will be marked by the GC and disposed of.
If you had added "t" to some kind of list (keeping a reference somewhere) then you could query it with Thread.IsAlive or Thread.ThreadState to figure out if that particular thread was still running or had been aborted.
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
If there are no other references to your MyThreadWorkerClass instances then they will be marked for garbage collection and eventually destroyed automatically.
Also, instead of:
dim t as new Thread(AddressOf MyThreadWorkerClass.DoWork
It would be:
dim t as new Thread(AddressOf tWorker.DoWork)
*Though you probably already knew that and this is just example code.