Hi All I want to read from a socket in an asynchronous way.
If I used the synchronous code below
StreamReader aReadStream = new StreamReader(aStream); String aLine = aReadStream.ReadLine(); // application might hand here
with a valid open connection, but no data available for reading, the application would hang at the ReadLine() function.
If I switch to the code below using Asynchronous Programming Model
NetworkStream aStream = aClient.GetStream(); while(true) { IAsyncResult res = aStream.BeginRead(data, 0, data.Length, null, null); if (res.IsCompleted){ Trace.WriteLine("here"); } else { Thread.Sleep(100); } }
In the same scenario what would happen? If there is no data to read, the variable res
would appear as Completed
or not?
Most important, when there is no data to read, all of my calls in that while loop to aStream.BeginRead()
are they scheduled continuously in the Thread Pool? If this is true am I risking to degrade the application performances because the Thread Pool has increased too much its size?
Thanks for the help
AFG