How to do it...

  1. Start debugging your multithreaded application after adding a break point somewhere in the code. You can access various debugging windows by going to Debug | Windows in Visual Studio:
  1. The first window available to you is the Threads window. Access it by going to Debug | Windows in Visual Studio or type Ctrl + D, T. In here, you can right-click on a thread to watch and flag it. If you have given your threads names, you will see those names appear in the Name column. To give your thread a name, modify the LockThreadExample() method created in an earlier recipe.
        public void LockThreadExample()
{
Task thread1 = Task.Factory.StartNew(() => ContendedResource(3));
Task thread2 = Task.Factory.StartNew(() => ContendedResource(5));
Task thread3 = Task.Factory.StartNew(() => ContendedResource(2));

int threadID = Thread.CurrentThread.ManagedThreadId;
Thread.CurrentThread.Name = $"New Thread{threadID}";

Task.WaitAll(thread1, thread2, thread3);
WriteLine("All tasks completed");
}

You will also be able to see the currently active thread in the debugger. It will be marked with a yellow arrow. Then, there is the managed ID, which is the same ID you will have used to create the unique thread name earlier on.

The Location column displays the current method that the thread is in. The Threads window allows you to view the stack of the thread by double-clicking on the Location field. You can also freeze and thaw threads. Freezing stops a thread from executing, while thawing allows the frozen thread to continue as normal.

  1. The Tasks window can be accessed by going to Debug | Windows or by holding down Ctrl Shift DK. To see this in action, place a break point in your LockThreadExample() method on the line that reads Task.WaitAll(thread1, thread2, thread3);. Debug your application again and look at the Status column for each thread created. The status of the task shows the status at that moment, and we can see that the three threads are Active, Blocked, and Scheduled:
  1. The Parallel Stacks window can be accessed by going to Debug | Windows in Visual Studio or by holding down Ctrl + D + S key. Here, you can see a graphical view of the tasks and threads. You can switch between the Threads and Tasks view by making a selection in the drop-down list in the upper-left corner of the Parallel Stacks window:
  1. Changing the selection to Tasks will show you the current tasks in the debug session:
  1. The next window, undoubtedly my favorite, is the Parallel Watch window. It is in fact identical to the standard Watch window in Visual Studio, but this watches values across all threads in your application. You can type any valid C# expression into Parallel Watch and see the values as they are at that moment in the debug session. Try it out by adding several break points and adding expressions into the Parallel Watch.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.234.28