c# - "bounded" BatchBlock => ActionBlock. How to complete the proper way? -


i'm trying use bounded batchblock linked action block. know when feeding of items in batchblock end , want trigger completion chain.

the problem is: if batchblock<t> of given boundedcapacity won't items fired in action block.

here sample of problem, should (well in understanding of tpl dataflow...) print 0 124 ends printing 0 99.

there must i'm missing... maybe boundedcapacity means "drop items when queue count on xxx..." if how can achieve guaranteed maximum memory consumption?

using system; using system.collections.generic; using system.threading; using system.threading.tasks.dataflow;  namespace consoleapplication {     class program     {         static void main(string[] args)         {             int itemscount = 125;             list<int> ints = new list<int>(itemscount);             (int = 0; < itemscount; i++)                 ints.add(i);              batchblock<int> batchblock = new batchblock<int>(50,new groupingdataflowblockoptions(){boundedcapacity = 100});             actionblock<int[]> actionblock = new actionblock<int[]>(intsbatch =>             {                 thread.sleep(1000);                 foreach (int in intsbatch)                     console.writeline(i);                            });             batchblock.linkto(actionblock, new dataflowlinkoptions() { propagatecompletion = true });              // feed batch block             foreach (int in ints)                 batchblock.post(i);             // don't know how end proper way... meaning should display 0 124 , not 0 99             batchblock.complete();             batchblock.triggerbatch();             actionblock.completion.wait();         }     } } 

post on block doesn't succeed. tries post message block if boundedcapacity reached fail , return false.

what can use sendasync instead returns awaitable task. if block has room message completes asynchronously. if doesn't block returns task complete when have room accept new message. can await task , throttle insertions:

async task mainasync() {     var ints = enumerable.range(0, 125).tolist();     var batchblock = new batchblock<int>(50, new groupingdataflowblockoptions { boundedcapacity = 100 });     var actionblock = new actionblock<int[]>(intsbatch =>     {         thread.sleep(1000);         foreach (var in intsbatch)             console.writeline(i);     });     batchblock.linkto(actionblock, new dataflowlinkoptions { propagatecompletion = true });      foreach (var in ints)         await batchblock.sendasync(i); // wait synchronously block accept.      batchblock.complete();     await actionblock.completion; } 

Comments