When working with multiple threads, it becomes important to find out which thread is executing at a particular time. This allows us to troubleshoot cross-threading issues as well as race conditions. Using the Threads window, we can check and work with threads while debugging. When you hit a breakpoint while debugging code in Visual Studio IDE, the thread window provides a table with each row containing information about active threads.
Now, let's explore how to debug threads using Visual Studio:
- Let's write the following code in Visual Studio:
for (int i = 0; i < 10; i++)
{
Task task = new TaskFactory().StartNew(() =>
{
Console.WriteLine($"Thread with Id
{Thread.CurrentThread.ManagedThreadId}");
});
}
- Create a breakpoint by pressing...