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

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as