Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using TPL Dataflow, can I cancel all posts and then add one?

With the TPL Dataflow library, I would like to do something like this:

myActionBlock.Post(newValue, cancelAllPreviousPosts: true);

It appears that the cancellation token on ActionBlock cancels the whole thing; I'd have to make a new ActionBlock if I set that one. Is it possible to do a partial cancellation with ActionBlock?

Posts that have not been processed yet should not be attempted. It would be nice if there was some cancellation token available to check in the currently-executing post.

like image 632
Brannon Avatar asked Feb 19 '14 18:02

Brannon


1 Answers

Take a look at BroadcastBlock<T>, which only holds the most recent item posted to it. You can put a broadcast block in front of an ActionBlock<T>.

While posting a new item to the broadcast block won't cancel the item currently being processed by the action block, it will overwrite any existing item already held by the broadcast block; in effect discarding any older messages not yet processed by the action block. When the action block completes its current item, it will take the most recent item posted to the broadcast block.

like image 144
Monroe Thomas Avatar answered Sep 20 '22 14:09

Monroe Thomas