2 回答

TA贡献1876条经验 获得超5个赞
您尚未设置BoundedCapacity,它控制输入缓冲区中可以等待的项目数。超过这将使SendAsync
等待。
您设置MaxNumberOfGroups属性,这是该块在拒绝接收任何其他输入之前将生成的组数。
从文档:
获取或设置应该由块生成的最大组数。
如果您希望您的块在输入缓冲区中保留例如 20 个块并等待,您应该设置 BoundedCapacity :
var sourceBlock = new BatchBlock<int>(10, new GroupingDataflowBlockOptions { BoundedCapacity = 20 });

TA贡献1820条经验 获得超10个赞
一旦达到最大值就await sourceBlock.SendAsync(i);不会暂停,因为该块会主动拒绝更多项目。发生这种情况时,SendAsync返回false指示该块将不接受新消息。如果你写出SendAsync调用的结果,你可以看到块停止接收新消息的位置:
Sending 0 to the source block
True
Sending 1 to the source block
True
Sending 2 to the source block
True
Sending 3 to the source block
True
Sending 4 to the source block
True
Sending 5 to the source block
True
Sending 6 to the source block
True
Sending 7 to the source block
True
Sending 8 to the source block
True
Sending 9 to the source block
True
Sending 10 to the source block
True
Sending 11 to the source block
True
Sending 12 to the source block
True
Sending 13 to the source block
True
Sending 14 to the source block
True
Sending 15 to the source block
True
Sending 16 to the source block
True
Sending 17 to the source block
True
Sending 18 to the source block
True
Sending 19 to the source block
True
Sending 20 to the source block
False
Sending 21 to the source block
False
Sending 22 to the source block
False
Sending 23 to the source block
False
Sending 24 to the source block
False
Sending 25 to the source block
False
Sending 26 to the source block
False
Sending 27 to the source block
False
Sending 28 to the source block
False
Sending 29 to the source block
False
Sending 30 to the source block
False
Sending 31 to the source block
False
Sending 32 to the source block
False
Sending 33 to the source block
False
Sending 34 to the source block
False
Sending 35 to the source block
False
Sending 36 to the source block
False
Sending 37 to the source block
False
Sending 38 to the source block
False
Sending 39 to the source block
False
Sending 40 to the source block
False
Sending 41 to the source block
False
Sending 42 to the source block
False
Sending 43 to the source block
False
Sending 44 to the source block
False
Sending 45 to the source block
False
Sending 46 to the source block
False
Sending 47 to the source block
False
Sending 48 to the source block
False
Sending 49 to the source block
False
Sending 50 to the source block
False
Sending 51 to the source block
False
Sending 52 to the source block
False
Sending 53 to the source block
False
Sending 54 to the source block
False
Sending 55 to the source block
False
Sending 56 to the source block
False
Sending 57 to the source block
False
Sending 58 to the source block
False
Sending 59 to the source block
False
Finished sending data to the source block
Received:
10 10
- 2 回答
- 0 关注
- 227 浏览
添加回答
举报