user420667
user420667

Reputation: 6710

At what point is the Thread.CurrentThread evaluated?

In the following code:

ThreadStart ts = new ThreadStart((MethodInvoker)delegate
        {
            executingThreads.Add(Thread.CurrentThread);
            // work done here.
            executingThreads.Remove(Thread.CurrentThread);
        });
        Thread t = new Thread(ts);
        t.Start();

Perhaps you can see that I'd like to keep track of the threads that I start, so I can abort them when necessary.

But I worry that the Thread.CurrentThread is evaluated from the thread that creates the Thread t, and thus aborting it would not abort the spawned thread.

Upvotes: 2

Views: 172

Answers (4)

CodesInChaos
CodesInChaos

Reputation: 108830

First of all I think it's a bad idea to abort threads, check the other answers/comments for the reasons. But I'll leave that aside for now.

Since your calls are inside a delegate they'll only be evaluated once the thread executes the content of that delegate. So the code works as you expect it to work and you get the thread on which the delegate executes, not the thread which created the delegate.

Of course your code isn't exception safe, you should probably put the remove into a finally clause.

executingThreads must be a thread safe collection, or you need to use locking.

Another way to keep track of the threads is to add the created thread to a collection from the creating thread, and then use the properties of that thread to check if the thread has already terminated. That way you don't have to rely on the thread keeping track itself. But this still doesn't fix the abortion problem.

Upvotes: 3

InBetween
InBetween

Reputation: 32780

Aborting threads is never a good idea. If you are 100% positive that whatever task you are performing in the thread you want to abort will not corrupt any state information anywhere else then you can probably get away with it, but its best to avoid doing so even in those cases. There are better solutions like flagging the thread to stop, giving it a chance to clean up whatever mess it may leave behind.

Anyhow, answering your question, Thread.CurrentThread is executing in the method invoked when the new thread starts executing, therefore it will return the new thread, not the thread where the new thread was created (if that makes sense).

Upvotes: 6

David Heffernan
David Heffernan

Reputation: 613311

In the code you have given, Thread.CurrentThread is called in the context of the thread t and not its creator.

Also, aborting threads is morally equivalent to killing puppies.

Upvotes: 6

Joe Enzminger
Joe Enzminger

Reputation: 11190

To answer your question, and without comment on the wisdom of aborting a thread (I agree with previous commenters by the way), Thread.CurrentThread, as you have written it, will do what you are expecting to do - it will represent the thread that is currently invoking your delegate, not the thread that created and started the thread.

Upvotes: 4

Related Questions