views:

333

answers:

6

Or is it okay to do something like this:

new Thread( new ThreadStart( delegate { DoSomething(); } ) ).Start();

?

I seem to recall that under such a scenario, the Thread object would be garbage collected, but the underlying OS thread would continue to run until the end of the delegate passed into it. I'm basically looking for ThreadPool functionality, but don't want the threads to be background threads (i.e. I want them to keep the app alive).

Update: According to Jason, the CLR actually keeps an internal reference to the Thread object, while running, so it won't be garbage collected until the thread exits.

+1  A: 

It depends. In the situation where the user can cancel the operation of your thread, you should keep the reference so the thread can be canceled when the user want. In other situations, there may be no need to store the reference.

m3rLinEz
Canceling is not necessary--it's a background (sync) operation, but I want it to keep the app alive at shutdown so it can perform a final sync before it quits. I was using the ThreadPool until I realized quitting the app aborted the threads.
chaiguy
A: 

It might be good to ask the question "How often can this thread be started up?" Is it per-application, per-class, per-object instance, or per-method invocation? This may tell you what kind of variable (if any) to store it in.

skiphoppy
Basically I would be executing that line on a schedule of every ~5 mins, and am concerned if it will leak, or if the threads will run properly.
chaiguy
Whether the thread will leak (or worse, simply loop to infinity) depends on the code it is executing. Whether it's OK to spin off an anon thread depends on the code it will be running.
Chris
+4  A: 

I have generally found that if I need to directly start a new thread the way you are in your example, rather than grabbing one from the thread pool, then it is a long running thread and I will need a reference to it later to kill it, monitor it, etc. For short run threads like invoking IO on a background thread, etc, I always use a thread pool thread (usually indirectly through a someDelete.BeginBlah(...) method call). When using a thread pool thread like this I prefer to NOT keep a reference around. I don't know if another programmer might inappropriately use a reference to that thread. If I don't need a reference, I don't keep it around to clutter up code.

Edit: To answer your edit about threads being garbage collected, this will not occur while the thread is running. The CLR keeps a reference to every running thread. The object representing the thread will NOT be collected.

Jason Jackson
Ah cool, not that it matters in this particular case, but presumably it WILL be collected after it exits, right?
chaiguy
Yes. A reference to the thread object is placed on the stack with the actual execution thread. Once the actual thread completes then there will be no reference to the thread object, if I recall my Richter correctly. I read that in either CLR via C# or C# in a Nutshell. Both great books.
Jason Jackson
A: 

Yes, you should, because you never know when you will have to change the code later to handle the thread in some way. That, and putting too much stuff on one line like that is just ugly.

So truthfully, you can do it your way, so the answer really comes down to code style preference.

Charles Graham
A: 

In addition to what "m3rLinEz" has posted above, another draw back is that if any exception occurs in your thread it will be hard to even detect such cases.

Sesh
+1  A: 

I have had a number of cases in production code where doing this has been appropriate. So, yes defining and starting a thread in one line without retaining a reference has it's place. I think keeping a reference "just in case" you redesign later and need it is failing the principle of creating the simplest thing that works.

And, to the second part, no it will not be GC'd while it is running; threads are root level objects from which the GCtor will chase down references. The Thread instance will only be GCd once it is no longer reachable by any running thread including the one which you start on it.

And beware leaking Thread instances which are created but never started. I believe they will hang around forever.

Software Monkey