Amazon.ca Widgets

Make your api faster with ThreadPool in ASP.NET

One easy way to make a server-side call faster, in your API, is to add some async procedures.  Sometimes, you want to call an external api (e.g. mailchimp…) or just send an e-mail, write a log, etc.

When your API client don’t need interaction from these calls, you can process them asynchronously.

The easiest way to add an async call is from the System.Threading.ThreadPool workspace.

As they say on their documentation, “The thread pool enables you to use threads more efficiently by providing your application with a pool of worker threads that are managed by the system.

And, it’s very easy to use.  This is a very simple example, in VB.NET:

Threading.ThreadPool.QueueUserWorkItem(
   sub
      ' Enter your async procedure here, 
      ' with callback and error handling if needed
   end sub)

That’s all!
That anonymous sub can access your caller parameters and other variables declared outside.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: