472,988 Members | 2,927 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,988 software developers and data experts.

Thread GC collection

I have the following code:
====================
public class Test
{
Thread thread;
ThreadStart threadStart;
int i;

public void Run()
{
threadStart = new ThreadStart(Work);
thread = new Thread(threadStart);
thread.Name = "" +(i++ );
thread.Start();
}

public void Work()
{
int i = 0;
while (i < 10)
{
Thread.Sleep(200);
Console.WriteLine(Thread.CurrentThread.Name);
}
}
}

void main(string[] args)
{
Test test = new Test();
for (int i = 0; i < 100; i++)
{
test.Run();
}
}

======================
My question is, each time Run() is called a new thread seems to be
started. Can anyone shed light onto why any existing thread running
for the "thread" member variable isn't aborted? Or does it just
restart "thread"? The thread.Name is different each time though.
Jul 2 '08 #1
5 1456
Becase you are asking it to... creating a new thread and changing the
"thread" reference doesn't affect the existing thread in the least -
it is still there, running happily. Threads are top-level objects as
far as the GC is concerned; a running thread will not be collected.
The fact that you have (or haven't) got a convenient reference to this
Thread instance is irrelevant. It wouldn't even matter if "thread" was
a method-variable and didn't exist outside of Run(), and it wouldn't
matter if we set "thread = null:" after calling Start().

There are several ways of interrupting a running thread, but the best
approaches tend to involve something graceful like a (volatile) flag
somewhere; things like Suspend() and Abort() have many associated
risks.
Jul 2 '08 #2
So what does the CLR do with the previous "thread" member variables?
Is it an issue creating new thread like this, instead of a new local
(to the method) one, or using a ThreadPool?
Jul 2 '08 #3
<pu****@googlemail.comwrote:
So what does the CLR do with the previous "thread" member variables?
There's only one variable involved, and its value is changed, just like
any other variable.
Is it an issue creating new thread like this, instead of a new local
(to the method) one, or using a ThreadPool?
There's no such thing as a "local" thread. Whether you should use the
thread pool or not depends on the circumstances.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 2 '08 #4
My question is, each time Run() is called a new thread seems to be
started. Can anyone shed light onto why any existing thread running
for the "thread" member variable isn't aborted? Or does it just
restart "thread"? The thread.Name is different each time though.

You are creating a thread, but you know that.

I think that you were expectig that as your code do not hld any
reference to the previous thread it will be recicled (or aborted). The
answer is that the thread will exist as long as it's running. Which
makes a lot of sense if you think about it. Some class in the
framework (the scheduler?) will hold a reference to it.
Jul 2 '08 #5
On Jul 2, 9:19*am, pug...@googlemail.com wrote:
So what does the CLR do with the previous "thread" member variables?
Is it an issue creating new thread like this, instead of a new local
(to the method) one, or using a ThreadPool?
When the thread ends the variables are collected in the usual way.

It works any way, IIRC creating a new thread is more costly than using
one of the thread in the pool. but I have no idea how much though.
Jul 2 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Grandma Wilkerson | last post by:
Hi, The documentation states that enumeration through a collection is inherently NOT thread-safe, since a thread which added/removed an item from said collection could screw up the thread that...
3
by: Grandma Wilkerson | last post by:
Hi, The documentation states that enumeration through a collection is inherently NOT thread-safe, since a thread which added/removed an item from said collection could screw up the thread that...
14
by: Jeff S. | last post by:
In a Windows Forms application I plan to have a collection of structs - each of which contains a bunch of properties describing a person (e.g., LastName, FirstName, EmployeeID, HomeAddress,...
14
by: joey.powell | last post by:
I am using VS2005 for a windows forms application. I need to be able to use a worker thread function to offload some processing from the UI thread. The worker thread will need access to a...
6
by: fniles | last post by:
I am using VB.NET 2003 and a socket control to receive and sending data to clients. As I receive data in 1 thread, I put it into an arraylist, and then I remove the data from arraylist and send it...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.