Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Another thread question

Posted on 2011-03-02
3
Medium Priority
?
261 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 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 tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
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…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

972 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