TPL TransformBlock Behavior

net 6 newbie 121 Reputation points
2024-05-12T16:58:17.37+00:00

I have created TransfromBlock of capacity 5. When I am trying to send 6 elements then the code portion like receiving of the block elements and count is not working. I expected that elements beyond the bounded capacity should be discarded/ignored and the rest should work. But it not working in such way. Is my understanding is wrong or it is the intended behavior?

var executiondataflowblockoptions = new ExecutionDataflowBlockOptions(){
    BoundedCapacity = 5,    EnsureOrdered = true,    MaxDegreeOfParallelism = 3};


var multiplyblock = new TransformBlock<int, int>(async (a) =>{
    try    {        if (a == 2)            
						throw new InvalidOperationException("Invalid Multiply opeartion");       
					 await Task.Delay(100);       
					 return a * a;    }    
	catch(Exception ex)   
 {        Console.WriteLine(ex.ToString());           return 0;    }}, 
executiondataflowblockoptions); 
await multiplyblock.SendAsync(25); 
await multiplyblock.SendAsync(2); 
await multiplyblock.SendAsync(14); 
await multiplyblock.SendAsync(47); 
await multiplyblock.SendAsync(77); 
await multiplyblock.SendAsync(64);

// Code not getting executed
 Console.WriteLine("Input Count : " + multiplyblock.InputCount);
 Console.WriteLine("Output Count : " + multiplyblock.OutputCount);
 for (var i = 0; i < 5; i++)
 {
     Console.WriteLine(multiplyblock.Receive<int>());
 }

 await multiplyblock.Completion;
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,240 questions
{count} votes