Steps to debug two or more threads in a common method | Visual Studio | C# | Threading | Static Issue

Steps to debug two or more threads in a common method

In the real-time development, you might encounter a scenario in which you have to debug a set of method or function for its behavior across the different number of threads. 

I encountered a similar problem today and I had to debug it through Visual Studio 2015. 

Scenario

I had an issue in my web application where 2 users (assume 'User-1' and 'User-2') entered their details (username/ password) and 'User-1' got logged into the system as 'User-2'. 'User-2' remained same as 'User-2'. Booommmmm! However, this issue was never reproduced by the QA Team but this was the burning issue for the client because User-2 was a superuser and client was afraid if this is happening in the production environment too. 

Long story short, I drilled down this issue to a static method, responsible for the login into the system was 'Private static common' variable. Dammit!!!

I had to log in through two different users and debug the same method in order to prove my theory. 

Solution

The solution had two parts, a few Visual Studio settings & some debugging tricks. 

Visual Studio Settings: 


1) Uncheck the option "Break all processes when one process breaks" in Visual studio settings. See below: 


2) Make sure Visual Studio is running as an Administrator so that you can attach the debugger. 

Tricks to debug: 

While debugging your solution, you will see a Thread ID in visual studio: 



Make a note of the Thread Id (22576 in our case), because it would be the primary key which we will use to identify our target thread while debugging. 

Now take control to the desired 'Line of code' which you want to examine (using F10 or F11).

Open Debug menu in Visual Studio

Debug -> Windows -> Threads 

Right click on the thread id & choose 'Freeze' option.



Now, press F10 or F5 and you are all set to debug another thread. This will remain freeze until you explicitly "Thaw" the thread or close debugging in Visual Studio.


*Feel free to leave comments & suggest improvements. Thanks

Comments

Popular posts from this blog

FastEndpoint vs Minimal APIs vs Controller

Kick start 2021 😃😃

Work with Raspberry PI Zero W