I've been doing a bit of TPL dataflow coding and am quite happy with the basics. The question I have is, how would I go about doing a TPL block that, besides reacting to its queue, also has a life on its own?
Like, a background task that runs on a permanent loop, polling the odd webservice or database every few seconds and emitting messages on its outputs when it sees fit?
The interface would be a source- and target block, but with no apparent connection between source messages and target messages.
Basically an "active" block.
So, here's what I did in the end: My FSM class consisted of a broadcast block, working as input, a bufferblock working as output and an async method with an endless loop running in the threadpool. The async method gets the current input value from the broadcastblock whenever needed, does its thing and posts a result to the bufferblock, in my case every second.
A user of that FSM class can link to the broadcast block's input and the bufferblock's output. The async method polls the input when needed and gets new values. Whatever it does, the result goes to the buffer block.
The interface then looks like
public interface IFsmBlock : IAsyncDisposable
{
ITargetBlock<string?> SearchParameterInputPort { get; }
ISourceBlock<string> FoundEntriesOutputPort { get; }
}
With possibly more than one input or output block. In this instance, the FSM treats the input port basically as a variable containing a value. If the input port should trigger something, that's easily possible by using an action block instead of the buffer block.