Solved

Another thread question

Posted on 2011-03-02
3
250 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 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

751 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