views:

475

answers:

7

I'm working on a .Net applications with multiple threads doing all sorts of things. When something goes wrong in production I want to be able to see which threads are running (by their managed name) and also be able to pause / kill them.

Anyway to achieve this ?

A: 

You can attach a managed debugger to view/freeze the threads, or use WinDbg with the SOS extensions if you want something lighter weight.

Rob Walker
+1  A: 

There's nothing built in to .net that will do this. If you want to programmatically iterate through your active threads, you have to register them somewhere on launch and either unregister them on completion or filter them before you act on them. We did a version of this and it requires a non-trivial amount of work.

Jekke
A: 

VS isn't always available (although a good option when is), and WinDbg UI isn't for the lite hearted.

I considered a in-program threads window, like VS has while debugging, but couldn't find a programmatic way to do this. Process.GetThreads returns very little usable data.

gpgemini
A: 

Jekke, should I decide to go with your direction, what are the unexpected complications ?

gpgemini
A: 

You can use ProcessExplorer to view running threads in a process by name and state.

http://technet.microsoft.com/en-us/sysinternals/bb896653.aspx

Use the "thread" tab of the "properties" window.

You can also kill threads.

olorin
A: 

What about remote debugging? It can be a bit finicky to setup because of security and ensuring you have the right debugging symbols though.

Remote Debugging Setup

Richard Nienaber
+2  A: 

Finding threads:

using System.Diagnostics;

ProcessThreadCollection threads = Process.GetCurrentProcess().Threads;

You can usually kill managed threads using Thread.Abort(). If they're in a Sleep, Wait or Join you may even be able to get away with the (less nasty) Thread.Interrupt().

rosenfield
Unfortunately, that returns a collection of ProcessThread, which is not the same as a System.Thread; the methods you referred to are on System.Thread. I'm not sure how to get from a ProcessThread to a System.Thread. :-/
Jeremy Dunck