Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/190.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Task.Factory.FromAsync(BeginRead,EndRead)是否在完成之前读取了整个数据块?_C#_Android_Android Asynctask_Tcpclient - Fatal编程技术网

C# Task.Factory.FromAsync(BeginRead,EndRead)是否在完成之前读取了整个数据块?

C# Task.Factory.FromAsync(BeginRead,EndRead)是否在完成之前读取了整个数据块?,c#,android,android-asynctask,tcpclient,C#,Android,Android Asynctask,Tcpclient,在我现在的代码中,我有如下内容: state.BytesRead += readPacket.Result; if (state.BytesRead < state.Data.Length) { Read(state); } else { nextRead(state); } 但它仍然没有运行。我在客户端和服务器之间发送了大量数据(长度超过1000个字符的字符串),但条件语句始终不会运行。看起来它总是一次读取所有字节 这是因为我使用的是Task.Factory.from

在我现在的代码中,我有如下内容:

state.BytesRead += readPacket.Result;

if (state.BytesRead < state.Data.Length)
{
    Read(state);
}
else
{
    nextRead(state);
}
但它仍然没有运行。我在客户端和服务器之间发送了大量数据(长度超过1000个字符的字符串),但条件语句始终不会运行。看起来它总是一次读取所有字节

这是因为我使用的是
Task.Factory.fromsync
,并且它只在读取所有字节时完成?下面是调用我文章顶部代码段的代码行:

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

我在FromAsync之前使用了异步回调,所有教程都要求在继续下一次读取之前检查是否读取了所有字节。FromAsync不再需要这样做了吗?它现在能自动处理吗?

Task.Factory.fromsync
只是围绕预先存在的异步BeginXXX/EndXXX对创建了一点框架。由于这些方法只是由框架包装和调用,而不是由您显式调用,因此它们的操作方法将保持不变。

您能解释为什么没有运行条件吗?即使发送大量数据并将缓冲区大小设置为非常小的数字,条件也不会运行。有必要吗?
Task<int>.Factory.FromAsync(state.Stream.BeginRead, state.Stream.EndRead, state.Data, state.BytesRead, state.Data.Length - state.BytesRead, state);