Create a Thread Wrapper Class

Problem

You want to remove the thread management code from your main thread and allow the task objects to manage their threads transparently.

Solution

Create a thread wrapper class that stores a reference to the thread and encapsulates the task-specific logic.

Discussion

One common design pattern with multithreading is to create a thread wrapper. This wrapper provides the typical methods you would expect in a Thread, like Start and Stop, along with all the task-specific code, and provides properties for required input values and calculated values.

Here’s an abstract base class that defines the basic structure of a well-behaved thread wrapper:

Public MustInherit Class ThreadWrapperBase ' This is the thread where the task ...

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.