.NET Framework Task Parallel Library and the Active Objects Pattern

Hiding an asynchronous or parallel process behind a
seemingly simple synchronous method call is common practice for a developer
trying to simplify an object's usability. Task Parallel
Library (TPL) is a good choice for the Parallel Computing
data structures often required for such a process. However, like all
development, following patterns and conventions eases the implementation burden
and improves code supportability.

Active Object is a common pattern for hiding access to
concurrent data structures and simplifying an object's interface. Like all patterns
Active Object is a prescriptive set of guidelines. A TPL Active Objects Pattern
implementation follows in the paragraphs below.

Active Object Overview

Developers may have many motivations for hiding concurrency.
One common motivation for employing an Active Object is: a developer may be
optimizing some existing class using concurrency and may not want to change how
the class is employed. Whatever the motivation, the pattern remains the same
and consists of the following elements:

A Proxy supporting client interaction

A Request message the Proxy creates to encapsulate the desired
invocation

A Scheduler that receives the Request messages from the Proxy and
maintains a Request message Queue. The Scheduler runs on a Thread separate from
the client containing the Proxy and handles execution scheduling.

A Servant performing the execution according to the Scheduler's
Request message

An optional return value or callback mechanism like a Task class
or Future. This scenario is beyond the scope of this article.

The graphic below depicts the interaction between the
components above.

Aside from the Threading and other components required to
schedule work; a shared data structure like the Request queue would require
guarded access so queuing Request messages don't collide with dequeuing requests.

The sample Active Object implementation is a Console
application that utilizes a Thread.Sleep to simulate a workload and prints a
string from the client to the Console. The client invocation code appears
below.

A Proxy may create different messages or include different
message contents according to the method invocation. The Active Object pattern
dictates using message passing instead of, for example, a delegate. Though a
developer could opt out of the pattern and use a delegate; it's important to
understand that message passing is a safer concurrency mechanism than a
delegate.

The Proxy posts the message to a queue residing inside the
Scheduler.

Scheduler and Servant

As stated earlier, the Scheduler manages a queue of Request
messages and handles scheduling the work execution. The Scheduler sample code
is below:

Conclusion

Active Object is a common pattern for hiding access to
concurrent data structures and simplifying an object's interface. Task Parallel
Library contains all the plumbing a developer will need to implement the
pattern.

Resources

About the Author

Jeffrey Juday

Jeff is a software developer specializing in enterprise application integration solutions utilizing BizTalk, SharePoint, WCF, WF, and SQL Server.
Jeff has been developing software with Microsoft tools for more than 15 years in a variety of industries including: military, manufacturing, financial services, management consulting, and computer security.
Jeff is a Microsoft BizTalk MVP.
Jeff spends his spare time with his wife Sherrill and daughter Alexandra.

Top White Papers and Webcasts

When individual departments procure cloud service for their own use, they usually don't consider the hazardous organization-wide implications. Read this paper to learn best practices for setting up an internal, IT-based cloud brokerage function that service the entire organization. Find out how this approach enables you to retain top-down visibility and control of network security and manage the impact of cloud traffic on your WAN.

U.S. companies are desperately trying to recruit and hire skilled software engineers and developers, but there is simply not enough quality talent to go around. Tiempo Development is a nearshore software development company. Our headquarters are in AZ, but we are a pioneer and leader in outsourcing to Mexico, based on our three software development centers there. We have a proven process and we are experts at providing our customers with powerful solutions. We transform ideas into reality.