开发者

Does Task.Factory.FromAsync(BeginRead, EndRead) read the entire data block before completing?

开发者 https://www.devze.com 2023-04-09 20:06 出处:网络
In my code right now I have something like this: state.BytesRead += readPacket.Result; if (state.BytesRead < state.Data.Length)

In my code right now I have something like this:

state.BytesRead += readPacket.Result;

if (state.BytesRead < state.Data.Length)
{
    Read(state);
}
else
{
    nextRead(state);
}

However, I noticed that the开发者_JS百科 first if statement never runs. I tried doing something like this to see if I could even get the conditional to ever run:

tcpClient.SendBufferSize = 16;
tcpClient.ReceiveBufferSize = 16;

But still it never runs. I am sending a lot of data (strings of over >1000 characters in length) between the client and the server but still, the conditional never runs. It looks like it just always just reads in all the bytes at once.

Is this because I am using Task.Factory.FromAsync and that it only completes upon reading in all the bytes? Here is the line of code that invokes the code snippet at the top of my post:

Task<int>.Factory.FromAsync(state.Stream.BeginRead, state.Stream.EndRead, state.Data, state.BytesRead, state.Data.Length - state.BytesRead, state);

I was using asynchronous callbacks before FromAsync and all the tutorials for that required checking if all the bytes were read before continuing on with the next read. Is this no longer necessary with FromAsync? Does it handle that automatically now?


Task.Factory.FromAsync merely creates a bit of scaffold around pre-existing async BeginXXX/EndXXX pairs. As the methods are merely wrapped and invoked by framework rather than being explicitly invoked by you, the method by which they operate will remain unchanged.

0

精彩评论

暂无评论...
验证码 换一张
取 消