Reputation: 1601
After identifying a particular thread of interest, is it possible to programmatically get its location at runtime?
In other words, I want to see which method this thread is currently executing:
var executingThread = System.Threading.Thread.CurrentThread;
I know I can find this information in the Threads window when debugging in VisualStudio, but I'm stumped as to whether this is possible to obtain in code.
My goal is to periodically log the location of a thread I'm monitoring in a long running process.
Upvotes: 4
Views: 561
Reputation: 35870
You'd be introducing a race condition by definition--by the time another thread observed that the observed thread was executing MethodX
it could have moved on to MethodY
--since it would be continually running. Or, you could observe MethodX
when you look, but it was spending most of its time in MethodY
. What you want to do can be done with debuggers and profilers--those will be your best bet and most reliable.
Upvotes: 1