Create a Reusable Task Processor

Problem

You need a dedicated thread that will process queue tasks and process them continuously.

Solution

Create a threaded class that stores tasks in a queue and monitors the queue continuously.

Discussion

A common pattern in distributed application design is a task processor: a dedicated thread that performs requested tasks on a first-in-first-out basis. This thread lives for the life of the application and uses an internal collection (typically a queue) to store requested tasks until it has a chance to process them.

Creating a task processor is fairly easy. However, to do it properly, you need to leverage the stop request pattern and thread wrapper pattern shown in recipes Stop a Thread Politely and Create a Thread ...

Get Microsoft® Visual Basic® .NET Programmer's Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.