Based on my reading and testing, with asynchronous sockets, the socket itself can be transferred using a state object (result of IAsyncResult), also if it stores the socket as a private field, it will be captured by callback methods.
I am wondering how is IAysnResult stored between BeginXXX and ReceiveXXX? It seems to me that after calling BeginXXX and completing the method, the state object will be deleted by GC if there is no link to it.
In the case of a private field, how is a private field divided between threads? (To my knowledge, the callback is done using the thread from the default thread pool, which will be considered as a new thread.)
Thank you very much, I hope that the questions themselves are clear.
source
share