I recently needed to develop some code that would read diagnostic information from a group of servers for a production application. This included reading a count of event log entries, reading performance counter values and also performing database queries to retrieve exception log information for the application. The idea was to provide a high level snapshot of the overall health of the application so at a glance support personnel could see if any issues were arising with the application (yes, I know, some home grown health monitoring).
In order to make the data retrieval efficient I wanted to take as many of the readings in parallel as possible . Prior to .NET 4.0 I would have looked at using the ThreadPool for this functionality and queueing separate user work items for each request. With .NET 4.0 however we now have use of the Task Parallel Library (TPL) which allows for more efficient use of system resources and also provides better programmatic control than is typically available when working directly with the thread pool.
In my particular scenario I have a group of servers that each have one or more of the above readings (event log count, performance counter or database query) that need to be retrieved. The result of each reading returns a similar output (numeric in this case – either integer or double). Since I want to execute these tasks in a concurrent manner, that’s where the TPL and the new Task class come into play. The Task class allows me to easily execute an asynchronous operation and return a result.
In my case I effectively want the retrieval of each data reading to be executed as a separate Task. I can also have each task return a result by instantiating a Task<TResult> (see How to: Return a Value from a Task for more details). I want each Task to return the value for the reading, along with the server name and reading name (this is used to group/correlate the readings after all tasks have executed, which I’ll explain shortly). The output of each task is therefore defined by the DataResult class shown below.
Since I know the number of tasks I need to execute based on my inputs (the number of servers and the number of readings) I simply create a array of tasks of type DataResult. I then iterate through the readings and servers, executing the appropriate method for each Task based on the type of reading being obtained.
Of primary concern above is the looping construct and the switch statement that instantiates each Task instance with the appropriate method target depending on the type of reading. The other code is primarily concerned with passing the appropriate input to the target method and setting things up for the correlation. The target methods for each type of reading take the measureInfo as input, containing information on the data to be retrieved, and return an instance of the DataResult class. The methods for both the event log and performance counter operations are shown below.
The returned DataResult instance ends up in the task’s Result property. Since I need to do some correlation once all the tasks have finished executing, I can simply do a Task.WaitAll on the array of tasks (NOTE: Accessing the Result property on the task instance will also block the calling thread until the task finishes). Once they’ve finished I can then loop through the tasks and perform my correlation using the Result property as shown.
That’s it. Multi-threaded functionality added to my app with only a few lines of code and much greater programmatic control by using Tasks, especially the ability to return a complex data type as the result of the execution of each task. This example just scratches the surface of the TPL and using it for Task Parallelism. For additional topics please see Parallel Programming in the .NET Framework.