Background Worker
It is now time to introduce the first complex pattern in this chapter – Background Worker. It establishes a background server module running on multiple threads, to which we can queue data packets for processing. This module has its own input queue (a blocking collection, of course!) that holds unprocessed data, and an output queue that holds the results of processing until they are returned to the main thread.
Later in this chapter, you’ll see that a background worker looks very similar to a single stage of a pipeline. This is true – in fact, a background worker is implemented as a wrapper around the pipeline pattern! It does, however, implement important functionality that is not part of the pipeline pattern – namely, it can cancel work requests while they wait to be processed. We will see in the example how that can be of use.
Another nice feature of the background worker is that it provides a clean way to set up and tear down...