views:

317

answers:

3

In java it is possible to get a snapshot of the stacktraces of all running threads. This is done with java.lang.Thread.getAllStackTraces() (it returns Map<Thread,StackTraceElement[]>).

How can the same thing be done with .net?

A: 

There is a StackTrace class

var trace = new System.Diagnostics.StackTrace(exception);

http://msdn.microsoft.com/en-us/library/system.diagnostics.stacktrace.aspx

TGadfly
A: 

You can loop on System.Diagnostics.Process.GetCurrentProcess().Threads and for each Thread create a StackTrace object with the .ctor that takes a Thread as its param.

Ariel
A: 

Try this one

var proc = System.Diagnostics.Process.GetCurrentProcess();
var threads = proc.Threads;

var res = new Dictionary<Thread, StackTrace>();
foreach (Thread thread in threads)
{
    var stackTrace = new StackTrace(thread, true);
    res.Add(thread, stackTrace);
}

In res u get the dictionary with the mapping u want :)

MarcosMeli
Running that code throws an InvalidCastException for me. Process.Threads returns a ProcessThreadCollection wich contains ProcessThread objects not Thread objects.Furthermore, there is no way to convert from a ProcessThread to a Thread since ProcessThread represents OS-threads while Thread is .net threads.
Hans Løken