views:

55

answers:

2

If i use thread like this:

void foo()
{
new Thread().Start();
}

since the Thread object is not referenced, will it be disposed by GC before the designated work is done?

+1  A: 

The thread should stay alive until its method(s) return.

Check out: http://stackoverflow.com/questions/81730/what-prevents-a-thread-in-c-from-being-collected

Corey Sunwold
so the CLR is thread-aware.
Benny
@Benny I believe so, but once the thread has done all the work it needs to do it should be handled by the GC
Corey Sunwold
+1  A: 

From MSDN

It is not necessary to retain a reference to a Thread object once you have started the thread. The thread continues to execute until the thread procedure is complete.

 
The System.Threading.Thread class is really just there for bookkeeping/management. It isn't the actual mechanism that creates/maintains threads. That's managed by the runtime and is CLI implementation specific (for example, the Mono implementation may differ dramatically in thread management.)

hemp