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.
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“.
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.
This will make your life easier while dealing with multithread application.