Hy, I'm using the SocketAsyncEventArgsPool described on MSDN.
For each TCP Client i have an own pool(Stack) of 50 SocketAsyncEventArgs to write from the server to the Client. So, this works fine, but on restart the Client or the Server i have a Function that is sending many messages to the Client and for each message one SocketAsyncEventArgs is taken from my pool. When there are too many messages my Pool is empty and there is no free SocketAsyncEventArgs Object for sending und this message will not be send to the Client.
Is there any possibility to avoid this without increase my pool??? Thanks!!!
If you don't want to increase the size of your pool and assuming you are correctly returning each
SocketAsyncEventArgsafter use you can use a BlockingCollection to hold the required number ofSocketAsyncEventArgs. The consumers will block when there are no more items to consume until an item is returned to the collection.Update
Here is some sample code that creates a
BlockingCollectionof size 1 and fires off some consumers to process simultaneously. Each consumer takes an item from the collection to process it and in the mean time the others are blocked onTakeuntil an item is added back to the collection.When processing, you'll probably need to do that in a
try/finallyblock to ensure the item is always added back after processing it if an exception gets thrown.To close the collection you call
CompleteAdding()and any blockedTakemethods will throw aInvalidOperationExceptionUsage
Output