I am writing a server/client socket app in C# on Windows 10 platform. The server side code and GUI code are running in the same process. On the server side I am trying to optimize my code as I can have 255 socket client connections. I have followed Microsoft “Synchronous Server Socket Example”. I have moved (from their example) all the logic of “public static void ReadCallback(IAsyncResult ar)” to:
Task.Run(() =>
{
ReadCallback(IAsyncResult ar);
});
.. , hoping that “ReadCallback” will be called from a different thread, therefore releasing call-back socket thread ASAP. Is that going to create a problem since I have now call ‘socket.EndReceive(ar)’ and ‘socket.BeginReceive(…)’ called possibly from different thread? The code is still working but I don’t know whether it is by accident or by my design. Please comment on that.
IAsyncResultcomes from APM,Task.Runis TPL, you shouldn't mix the two approaches.Task.Runwill run on the thread pool,ReadCallbackwill probably run on different thread pool thread - Pavel Anikhouski