Flag your own code in multithreading debugging

In previous tip, we have seen how to debug multithread application in Visual Studio.  Debugging multithread application can be simple as well as complex depends on the application complexity. For a small application, where there are only few threads are running you can easily identify and trace them, but when program is complex it could be difficult to identify the treads. Moreover, the .Net Frameworks having it own threads running and there would be several threads from .NET Runtime and environments. In this situation  it will be very  difficult for you to identify which thread is part of your code or which are not related to your code.

For an example, consider the below code block.

class ThreadTest
{
static void Main()
{
Thread t = new Thread(new ThreadStart(Go));
t.Name = "Thread 1";
Thread t1 = new Thread(new ThreadStart(Go));
t1.Name = "Thread 2";
t.Start();
t1.Start();
Go();
}
static void Go()
{
Console.WriteLine("hello!");
}
}

You will find only three thread which are actually related to your code and they are

  • MainThread
  • Thread 1
  • Thread 2

Rest if them are from runtime, hosting process or from the system threads as shown in image below.

Multithreading_debugging_window

Now, just to identify your code related thread,  Thread window gives you very easy features to set the “Flag” for all the threads which are part of your code. For that, you need to just flag your thread by option “Flag Just My Code“.

Multithreading_debugging_window_FlagMyCode

Once you select the “Flag Just My Code”  option, the debugging window will only highlight the thread which are related to your code. Refer the following image, you will find MainThread, Thread 1 and Thread are highlighted.

Multithreading debugging window - FlagMyCode Highlighted

This will make your life easier while dealing with multithread application.

Abhijit Jana

Abhijit runs the Daily .NET Tips. He started this site with a vision to have a single knowledge base of .NET tips and tricks and share post that can quickly help any developers . He is a Former Microsoft ASP.NET MVP, CodeProject MVP, Mentor, Speaker, Author, Technology Evangelist and presently working as a .NET Consultant. He blogs at http://abhijitjana.net , you can follow him @AbhijitJana . He is the author of book Kinect for Windows SDK Programming Guide.