Solved

Another thread question

Posted on 2011-03-02
3
243 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now