devxlogo

A Custom Proxy that Handles Thread Creation and Object Pooling

A Custom Proxy that Handles Thread Creation and Object Pooling

enerally, when a good programmer has to design a type (or a hierarchy of types), he does an analysis of the problem and then starts to write properties and methods that describe the type behavior. If a method requires long processing time, it is the perfect candidate for being called in a separate thread. To handle asynchronous method calls you often have to write the same code to create threads and pass data to them. The .NET Framework provides delegate types that allow you to handle both synchronous and asynchronous calls through the BeginInvoke/EndInvoke idiom. The main purpose of this article is to show you a different approach to this problem, by implementing a custom proxy that delegates each method call of a type to a thread of a thread pool, by using declarative programming at design time. After this, we will extend the proxy capabilities by includingThe ThreadProxy class
Now we know much more about proxies and how they are implemented inside the .NET remoting infrastructure and so we enter in the main subject of the article. The basic idea is to write a custom proxy that intercepts the method call on a Type and, based on a custom property definition applied at method level, it executes the method in another thread on a type instance that can be taken from an object pool. You can refer to the source code related to this article.

First of all let examine the declarations.vb file.

In this file are declared interfaces, custom attributes and other useful class declarations. At this moment we will focus on IThreadPoolHandler interface. This interface is used by the proxy to execute the current method on another thread.

The library also includes a default implementation of this interface (CLRThreadPoolHandler) using the CLR ThreadPool.

You are free to provide your own implementation using another thread creation mechanism or a custom thread pool (see [4] for further details) simply re-implementing IThreadPoolHandler interface.

There is also a custom attribute declaration (ThreadMethodAttribute) that is used by ThreadProxy only to check if a method must be executed on another thread.

Now let’s see the ThreadProxy class implementation (ThreadProxy.vb).

ThreadProxy type has three constructors:

Public Sub New(ByVal supportedType As Type) Public Sub New(ByVal supportedType As Type, _    ByVal clbk As AsyncCallback, ByVal state As Object)Public Sub New(ByVal supportedType As Type, _    ByVal threadHandler As IThreadPoolHandler, _    ByVal clbk As AsyncCallback, ByVal state As Object) 

The first constructor accepts only a System.Type parameter

devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist