Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Another thread question

Posted on 2011-03-02
3
Medium Priority
?
260 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 86

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 86

Accepted Solution

by:
Mike Tomlinson earned 2000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

670 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