Solved

Another thread question

Posted on 2011-03-02
3
246 Views
Last Modified: 2012-05-11
Lets say I have a class like this

public class MyThreadWorkerClass

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?

0
Comment
Question by:rutledgj
  • 2
3 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 35023732
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
 

Author Comment

by:rutledgj
ID: 35023749
So does the new t as Thread also get marked for GC? Are both objects marked for gc when the thread completes?
0
 
LVL 85

Accepted Solution

by:
Mike Tomlinson earned 500 total points
ID: 35023794
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

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Exit the loop 4 49
VB.Net Report Printing Issue 3 54
vb.net cycle through results of a sql query and capture variables 3 18
VS 2015 is giving [No relevant source lines] 2 26
Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question