Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Filtering Threads in a Multi-Project Solution

5.00/5 (1 vote)
15 Aug 2012CPOL2 min read 9.8K  
Effective way of debugging multi-threaded application in a multi-project solution
This tip shows an effective way of debugging where you need to debug threads that are created in a particular project in an enterprise application.

Introduction

In a typical enterprise application, you will usually have more than one project, each of which serves a unique purpose. If this application is making use of multiple threads, there may be a situation where you need to debug threads that are created in a particular project. This tip shows an effective way of debugging in this type of situation.

Let’s start by discussing our sample solution. This is a simple solution, but it does suffice to introduce the concept. This solution consists of two projects; first one is called CallRouter which is a console-application. Other project is a class library called ClientManager. Both of these projects are creating few threads also. The code for CallRouter is as follows. Notice that threads created in this project have names prefixed with RouteProcessingWorker.

C#
namespace CallRouter
{
    class Program
    {
        static void Main(string[] args)
        {
            ClientManager.ClientManager CM = new ClientManager.ClientManager();
            new Thread(new ThreadStart(CM.Initialize)).Start();
 
            Thread[] RouteProcessingWorkers = new Thread[25];
            for (int i = 0; i < RouteProcessingWorkers.Length; i++)
            {
                Thread t = new Thread(new ThreadStart(RouteProcessor));
                t.Name = "RouteProcessingWorker" + i.ToString();
                t.Start();
            }
 
            Console.WriteLine("Route and Client Processing in progress");
        } 

        static void RouteProcessor()
        {
            Thread.Sleep(3000);
        }
    }
} 

The code for ClientManager is as follows. Notice that threads created in this project are prefixed with ClientProcessingWorker.

C#
namespace ClientManager
{
    public class ClientManager
    {
        public ClientManager() { }
        
        public void Initialize()
        {
            Thread[] ClientProcessingWorkers = new Thread[5];
            for (int i = 0; i < ClientProcessingWorkers.Length; i++)
            {
                Thread t = new Thread(new ThreadStart(ClientProcessor));
                t.Name = "ClientProcessingWorker" + i.ToString();
                t.Start();
            }
        }
 
        void ClientProcessor()
        {
            Thread.Sleep(5000);
        }
    }
}

Let's say you need to debug threads that are created in ClientManager module. For this purpose, you can add a filter in Threads windows to show you all threads created in ClientManager module. In order for adding this filter, you first need to flag all threads created in your custom module by selecting "Flag Custom Module Selection" option as shown below:

Image 1

You will get the following dialog where you can select the module that you wanted to add as filter.

Image 2

Now if you run the application and go in Threads windows, all the threads created in ClientManager module will be flagged as shown below:

Image 3

In order to view only threads in ClientManager module, you can group threads by choosing Group by Flag Indicator option as shown below:

Image 4

Now you can minimize all the group except the one for ClientManager as shown below and focus only on the threads you are interested in debugging.

Image 5

I hope you find this tip helpful.

History

  • 15th August, 2012: Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)