Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Monitoring C# Threads - Which does what/when

As everybody, I am used to debugging my code in VS in step-by-step mode. Well, now that I have an application with many Background Workers everywhere, I am not in Kansas anymore.

What is the most efficient way to debug threaded applications and be able to monitor each and every thread to keep track of what's happening all over the code?

As of now, I stick to good ol' debugging using separate logger instances for each Thread, but this is slowly becoming a nightmare and I'll soon be drowning into my own logs.

like image 751
Mehdi LAMRANI Avatar asked Dec 01 '10 21:12

Mehdi LAMRANI


2 Answers

Don't try to debug everything all at once. Narrow your focus to a particular behavior in one thread or pair of threads that interact around some mutex lock. If accessing a shared resource is the problem, set breakpoints around use of that resource (which should be in common code, not all over the place).

If you just want to see that thread 3 completed before thread 1, or that thread 2 used up all its work items and is sitting idle, use logs for that.

You can also use the VS Threads view to see what each thread is doing whenever the process is stopped at any breakpoint on any thread. This can give you some insight into what all the threads are doing at any given instant.

like image 107
dthorpe Avatar answered Oct 01 '22 04:10

dthorpe


A small tip that might ease your pain is to use Visual Studio to freeze threads that you are not interested in. Then when you tell the debugger to continue, the frozen threads will never execute and will not hit breakpoints and confuse you.

Maybe you can use this method to allow only the threads you are debugging to work. E.g. keep one thread that enqueues and one thread that dequeues active, but freeze everything else.

You can freeze/thaw threads from Visual Studio's Threads window, by right-clicking on a thread.

like image 22
Ran Avatar answered Oct 01 '22 04:10

Ran