Can anyone tell me if I'm likely to run into unintended behavior if I use anonymous methods with Async I/O?
As an example:
Action<Socket> acceptedHandler = DoAccept
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
e.Completed += ((sender, ea) => acceptedHandler(ea.AcceptSocket));
// Server is a Socket
if 开发者_运维知识库(!Server.AcceptAsync(e))
acceptedHandler(e);
The same applies to BeginXXX/EndXXX async I/O.
From the code snippet you pasted it doesn't seem like there would be any issue. The only time to worry about anonymous methods is when you are capturing variables.
There is nothing to worry about when using anonymous methods. Your example is actually a good example of when to use them. Indecently remember to properly use the SocketAsyncEventArgs class. I am hoping your example is extremely contrived.
精彩评论