.NET 异步流读/写

2023-11-29

我一直在尝试解决这个“并发编程”考试练习(C#):

知道Stream类包含int Read(byte[] buffer, int offset, int size) and void Write(byte[] buffer, int offset, int size)方法,在 C# 中实现NetToFile复制从接收到的所有数据的方法NetworkStream net实例到FileStream file实例。为了进行传输,请使用异步读取和同步写入,避免一个线程在读取操作期间被阻塞。传输结束时net读操作返回值0。为了简化,不需要支持操作的受控取消。

void NetToFile(NetworkStream net, FileStream file);

我一直在尝试解决这个练习,但我正在努力解决与问题本身相关的问题。但首先,这是我的代码:

public static void NetToFile(NetworkStream net, FileStream file) {
    byte[] buffer = new byte[4096]; // buffer with 4 kB dimension
    int offset = 0; // read/write offset
    int nBytesRead = 0; // number of bytes read on each cycle

    IAsyncResult ar;
    do {
        // read partial content of net (asynchronously)
        ar = net.BeginRead(buffer,offset,buffer.Length,null,null);
        // wait until read is completed
        ar.AsyncWaitHandle.WaitOne();
        // get number of bytes read on each cycle
        nBytesRead = net.EndRead(ar);

        // write partial content to file (synchronously)
        fs.Write(buffer,offset,nBytesRead);
        // update offset
        offset += nBytesRead;
    }
    while( nBytesRead > 0);
}

我的问题是,在问题陈述中说:

要进行传输,请使用异步 读取和同步写入,避免 一个线程在读取期间被阻塞 运营

我不太确定我的解决方案是否实现了本练习中所需的效果,因为我正在使用AsyncWaitHandle.WaitOne()等待异步读取完成。

另一方面,我并没有真正弄清楚在这种情况下什么是“非阻塞”解决方案,因为FileStreamwrite 是要同步进行的......要做到这一点,我必须等到NetworkStream阅读完成以继续FileStream写作,不是吗?

你能帮我解决这个问题吗?


[编辑1]Using callback解决方案

好吧,如果我明白的话米切尔·塞勒斯 and willvv回答说,有人建议我使用回调方法将其变成“非阻塞”解决方案。这是我的代码:

byte[] buffer; // buffer

public static void NetToFile(NetworkStream net, FileStream file) {
    // buffer with same dimension as file stream data
    buffer = new byte[file.Length];
    //start asynchronous read
    net.BeginRead(buffer,0,buffer.Length,OnEndRead,net);
}

//asynchronous callback
static void OnEndRead(IAsyncResult ar) {
    //NetworkStream retrieve
    NetworkStream net = (NetworkStream) ar.IAsyncState;
    //get number of bytes read
    int nBytesRead = net.EndRead(ar);

    //write content to file
    //... and now, how do I write to FileStream instance without
    //having its reference??
    //fs.Write(buffer,0,nBytesRead);
}

正如您可能已经注意到的,我被困在回调方法上,因为我没有对FileStream我想调用“Write(...)”方法的实例。

此外,这不是一个线程安全的解决方案,因为byte[]字段是公开的并且可以在并发之间共享NetToFile调用。我不知道如何在不暴露这个问题的情况下解决这个问题byte[]外部范围中的字段......我几乎可以肯定它可能不会以这种方式暴露。

我不想使用 lambda 或匿名方法解决方案,因为这不在“并发编程”课程的课程内容中。


尽管帮助人们完成家庭作业有悖常理,但考虑到这已经有一年多了,以下是实现这一目标的正确方法。所有你需要的overlap您的读/写操作 - 不需要产生额外的线程或任何其他操作。

public static class StreamExtensions
{
    private const int DEFAULT_BUFFER_SIZE = short.MaxValue ; // +32767
    public static void CopyTo( this Stream input , Stream output )
    {
        input.CopyTo( output , DEFAULT_BUFFER_SIZE ) ;
        return ;
    }
    public static void CopyTo( this Stream input , Stream output , int bufferSize )
    {
        if ( !input.CanRead ) throw new InvalidOperationException(   "input must be open for reading"  );
        if ( !output.CanWrite ) throw new InvalidOperationException( "output must be open for writing" );

        byte[][]     buf   = { new byte[bufferSize] , new byte[bufferSize] } ;
        int[]        bufl  = { 0 , 0 }                                       ;
        int          bufno = 0 ;
        IAsyncResult read  = input.BeginRead( buf[bufno] , 0 , buf[bufno].Length , null , null ) ;
        IAsyncResult write = null ;

        while ( true )
        {

            // wait for the read operation to complete
            read.AsyncWaitHandle.WaitOne() ; 
            bufl[bufno] = input.EndRead(read) ;

            // if zero bytes read, the copy is complete
            if ( bufl[bufno] == 0 )
            {
                break ;
            }

            // wait for the in-flight write operation, if one exists, to complete
            // the only time one won't exist is after the very first read operation completes
            if ( write != null )
            {
                write.AsyncWaitHandle.WaitOne() ;
                output.EndWrite(write) ;
            }

            // start the new write operation
            write = output.BeginWrite( buf[bufno] , 0 , bufl[bufno] , null , null ) ;

            // toggle the current, in-use buffer
            // and start the read operation on the new buffer.
            //
            // Changed to use XOR to toggle between 0 and 1.
            // A little speedier than using a ternary expression.
            bufno ^= 1 ; // bufno = ( bufno == 0 ? 1 : 0 ) ;
            read = input.BeginRead( buf[bufno] , 0 , buf[bufno].Length , null , null ) ;

        }

        // wait for the final in-flight write operation, if one exists, to complete
        // the only time one won't exist is if the input stream is empty.
        if ( write != null )
        {
            write.AsyncWaitHandle.WaitOne() ;
            output.EndWrite(write) ;
        }

        output.Flush() ;

        // return to the caller ;
        return ;
    }


    public static async Task CopyToAsync( this Stream input , Stream output )
    {
        await input.CopyToAsync( output , DEFAULT_BUFFER_SIZE ) ;
        return;
    }

    public static async Task CopyToAsync( this Stream input , Stream output , int bufferSize )
    {
        if ( !input.CanRead ) throw new InvalidOperationException( "input must be open for reading" );
        if ( !output.CanWrite ) throw new InvalidOperationException( "output must be open for writing" );

        byte[][]     buf   = { new byte[bufferSize] , new byte[bufferSize] } ;
        int[]        bufl  = { 0 , 0 } ;
        int          bufno = 0 ;
        Task<int>    read  = input.ReadAsync( buf[bufno] , 0 , buf[bufno].Length ) ;
        Task         write = null ;

        while ( true )
        {

            await read ;
            bufl[bufno] = read.Result ;

            // if zero bytes read, the copy is complete
            if ( bufl[bufno] == 0 )
            {
                break;
            }

            // wait for the in-flight write operation, if one exists, to complete
            // the only time one won't exist is after the very first read operation completes
            if ( write != null )
            {
                await write ;
            }

            // start the new write operation
            write = output.WriteAsync( buf[bufno] , 0 , bufl[bufno] ) ;

            // toggle the current, in-use buffer
            // and start the read operation on the new buffer.
            //
            // Changed to use XOR to toggle between 0 and 1.
            // A little speedier than using a ternary expression.
            bufno ^= 1; // bufno = ( bufno == 0 ? 1 : 0 ) ;
            read = input.ReadAsync( buf[bufno] , 0 , buf[bufno].Length );

        }

        // wait for the final in-flight write operation, if one exists, to complete
        // the only time one won't exist is if the input stream is empty.
        if ( write != null )
        {
            await write;
        }

        output.Flush();

        // return to the caller ;
        return;
    }

}

Cheers.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

.NET 异步流读/写 的相关文章

  • 机器Epsilon精度差异

    我正在尝试计算 C 中双精度数和浮点数的机器 epsilon 值 作为学校作业的一部分 我在 Windows 7 64 位中使用 Cygwin 代码如下 include
  • 如何从 Visual Studio 将视图导航到其控制器?

    问题是解决方案资源管理器上有 29 个项目 而且项目同时具有 ASP NET MVC 和 ASP NET Web 表单结构 在MVC部分中 Controller文件夹中有大约100个子文件夹 每个文件夹至少有3 4个控制器 视图完全位于不同
  • 随着时间的推移,添加到 List 变得非常慢

    我正在解析一个大约有 1000 行的 html 表 我从一个字符串中添加 10 个字符串 td 每行到一个list td
  • 访问外部窗口句柄

    我当前正在处理的程序有问题 这是由于 vista Windows 7 中增强的安全性引起的 特别是 UIPI 它阻止完整性级别较低的窗口与较高完整性级别的窗口 对话 就我而言 我想告诉具有高完整性级别的窗口进入我们的应用程序 它在 XP 或
  • ASP.NET Core 3.1登录后如何获取用户信息

    我试图在登录 ASP NET Core 3 1 后获取用户信息 如姓名 电子邮件 id 等信息 这是我在登录操作中的代码 var claims new List
  • 在 Unity 中实现 Fur with Shells 技术

    我正在尝试在 Unity 中实现皮毛贝壳技术 http developer download nvidia com SDK 10 5 direct3d Source Fur doc FurShellsAndFins pdf Fins 技术被
  • 为什么这个字符串用AesCryptoServiceProvider第二次解密时不相等?

    我在 C VS2012 NET 4 5 中的文本加密和解密方面遇到问题 具体来说 当我加密并随后解密字符串时 输出与输入不同 然而 奇怪的是 如果我复制加密的输出并将其硬编码为字符串文字 解密就会起作用 以下代码示例说明了该问题 我究竟做错
  • 使用.Net/C# 计算集合的频率分布

    是否有一种快速 简单的方法来使用 Linq 或其他方式计算 Net 集合的频率分布 例如 任意长的 List 包含许多重复项 遍历列表并计算 跟踪重复次数的巧妙方法是什么 查找列表中重复项的最简单方法是将其分组 如下所示 var dups
  • C# xml序列化必填字段

    我需要将一些字段标记为需要写入 XML 文件 但没有成功 我有一个包含约 30 个属性的配置类 这就是为什么我不能像这样封装所有属性 public string SomeProp get return someProp set if som
  • 实例化类时重写虚拟方法

    我有一个带有一些虚函数的类 让我们假设这是其中之一 public class AClassWhatever protected virtual string DoAThingToAString string inputString retu
  • LINQ:使用 INNER JOIN、Group 和 SUM

    我正在尝试使用 LINQ 执行以下 SQL 最接近的是执行交叉联接和总和计算 我知道必须有更好的方法来编写它 所以我向堆栈团队寻求帮助 SELECT T1 Column1 T1 Column2 SUM T3 Column1 AS Amoun
  • 引用的程序集自动由 Visual Studio 替换

    我有 2 个项目 一个可移植类库和一个常规单元测试项目 在可移植类库中 我使用 NuGet 来引用 Microsoft BCL 可移植包 它附带 2 个程序集 System Threading Tasks dll and System Ru
  • 为什么使用小于 32 位的整数?

    我总是喜欢使用最小尺寸的变量 这样效果就很好 但是如果我使用短字节整数而不是整数 并且内存是 32 位字可寻址 这真的会给我带来好处吗 编译器是否会做一些事情来增强内存使用 对于局部变量 它可能没有多大意义 但是在具有数千甚至数百万项的结构
  • 如何在 Linq to SQL 中使用distinct 和 group by

    我正在尝试将以下 sql 转换为 Linq 2 SQL select groupId count distinct userId from processroundissueinstance group by groupId 这是我的代码
  • 编译时展开 for 循环内的模板参数?

    维基百科 here http en wikipedia org wiki Template metaprogramming Compile time code optimization 给出了 for 循环的编译时展开 我想知道我们是否可以
  • 有没有办法让 doxygen 自动处理未记录的 C 代码?

    通常它会忽略未记录的 C 文件 但我想测试 Callgraph 功能 例如 您知道在不更改 C 文件的情况下解决此问题的方法吗 设置变量EXTRACT ALL YES在你的 Doxyfile 中
  • 在 WPF 中使用 ReactiveUI 提供长时间运行命令反馈的正确方法

    我有一个 C WPF NET 4 5 应用程序 用户将用它来打开某些文件 然后 应用程序将经历很多动作 读取文件 通过许多插件和解析器传递它 这些文件可能相当大 gt 100MB 因此这可能需要一段时间 我想让用户了解 UI 中发生的情况
  • 相当于Linux中的导入库

    在 Windows C 中 当您想要链接 DLL 时 您必须提供导入库 但是在 GNU 构建系统中 当您想要链接 so 文件 相当于 dll 时 您就不需要链接 为什么是这样 是否有等效的 Windows 导入库 注意 我不会谈论在 Win
  • C++ 继承的内存布局

    如果我有两个类 一个类继承另一个类 并且子类仅包含函数 那么这两个类的内存布局是否相同 e g class Base int a b c class Derived public Base only functions 我读过编译器无法对数
  • 为什么C++代码执行速度比java慢?

    我最近用 Java 编写了一个计算密集型算法 然后将其翻译为 C 令我惊讶的是 C 的执行速度要慢得多 我现在已经编写了一个更短的 Java 测试程序和一个相应的 C 程序 见下文 我的原始代码具有大量数组访问功能 测试代码也是如此 C 的

随机推荐

  • ng-repeat 动画不起作用

    我的 ng repeat 动画似乎不起作用 这是笨蛋 http plnkr co edit kYtzM9d0rzGmrniybz9c p preview 任何输入 1 您已注册两个模块 And Remove ng app来自 html 标签
  • 按钮悬停颜色更改动画 c#

    我创建了一个 Windows 窗体 它有 3 个按钮 因此 我想通过使用 mouseenter 事件来更改颜色的一键式按钮 工作正常 但我需要通过淡入或淡出的过渡来改变颜色 任何对此问题有答案的人请告诉我下面的代码 我将向您展示我的 mou
  • 通过 aws.push 部署到 Elastic Beanstalk 时保留某些现有文件

    我们在 EC2 服务器上的应用程序目录中有 SOLR 而不是在本地 git 存储库中 当我们将本地存储库推送到 EC2 时 它会删除 Solr 目录 我们已将目录名称放入 gitignore 文件中 但它仍然覆盖 SOLR 目录 我们如何防
  • IE 7 CTRL + 单击打开一个新窗口 - 如何抑制它?

    Is it possible to suppress the default IE 7 functionality when CTRL click on link opens a new window if so how Thanks Th
  • 如何在 SQL Server 2008 中从 IP 地址获取计算机名称?

    我必须从 SQL Server 中的 IP 地址获取计算机名称 我可以做些什么来完成我的任务 DB IPAdd MachineName Query select MachineName from DBTable where IPAdd yo
  • 在安卓中搜索

    我有一个应用程序 我希望在其中添加搜索功能 我正在尝试按照中所述实施developer android但是当我单击模拟器中的搜索时 活动没有启动 问题是什么 SearchActivity java public class SearchAc
  • 在 Spring Boot 中禁用自动日志记录配置

    我使用 spring boot 1 2 1 RELEASE 并注意到 spring 在启动时自动更改我的 log4j 配置 这是我的 春季 依赖项
  • 用于设置类的 CKEditor 插件

    我想做的是类似于本机前景色 背景颜色对话框 不同之处在于 它将直接在工具栏中包含带有颜色的按钮 所以一个插件必须有多个按钮 具有不同的样式 颜色 另一个问题是 这个原生插件设置了 CSScolor and background color特
  • 什么是“装饰器”以及它们如何使用?

    我很好奇 AngularJS 中的装饰器到底是什么 除了装饰器中的简介之外 网上没有太多关于装饰器的信息AngularJS 文档并在一个简短的 尽管有趣的 提及Youtube 视频 正如 Angular 的人所说 装饰器是 服务的装饰 允许
  • 音频和视频等大文件的数据库

    我的应用程序创建了大量文件 每个文件最大 100MB 目前我们将这些文件存储在运行良好的文件系统中 但我想知道是否有更好的解决方案将文件存储在某种文件数据库中 数据库的一个简单优点是它可以分割文件并存储在小块中 而不是一个 100mb 的文
  • 使用 shebang 通过 subprocess.call 执行 python 脚本

    我正在用 Python 3 编写一个 某种程度上 模块化应用程序 我想从中运行任意程序 该程序是在运行时指定的 而不一定是 python 脚本 所以我用例如 subprocess call spam i eggs o ham If spam
  • 带有自定义标签的 CSS 宽度

    我无法获取适用于我的自定义标签的宽度和高度属性 请参阅下面的代码 CSS x slider width 1000px height 300px border 1px black solid background color 0000ff H
  • 在.NET中获取用户的Exchange服务器和电子邮件地址

    嗨 我想知道我的用户的 Exchange 服务器的地址 假设她位于典型的 Windows 办公网络中 这是在 C 应用程序中 我已经有了用户的电子邮件地址 我在以下位置找到了它System DirectoryServices Account
  • 类依赖工具

    我正在寻找一个 最好是开源 工具 在给定大量 C C 代码的情况下 该工具将生成类 C 和 或文件 C 之间依赖关系的可视化或 XML 图表 这个想法是 如果您必须将代码转换为另一种语言 您希望能够首先编译最低级别的类 然后从那里开始构建
  • 在 Apache Flink 中使用 DynamoDB 流

    有人尝试过在 Apache Flink 中使用 DynamoDB 流吗 Flink 有一个 Kinesis 消费者 但我正在寻找如何直接使用 Dynamo 流 DataStream
  • Windbg:是否可以在我自己的程序中嵌入Windgb引擎?

    我想编写一个调试 诊断工具 它可以调用 Windbg 函数来检查转储文件 而不是编写 Windbg 扩展 这可能吗 有什么参考吗 多谢 您可以使用 dbghelp dll 中实现的调试 API 而不是 WinDbg 它记录在MSDN 该参考
  • 用指针修改字符串[重复]

    这个问题在这里已经有答案了 这两个代码必须改变字符2在角色中 4 int main int argc char argv char s hello s 2 4 printf s n s return 0 当我运行此命令时 我会遇到分段错误
  • Python 和 MySQLdb:表替换导致语法错误

    我需要时不时地动态更改表和变量 所以我写了一个像这样的python方法 selectQ SELECT FROM s WHERE s s self db execute selectQ self table self columnSpecNa
  • F# 中“>>”运算符的语义

    在微软的F 样本 他们使用 gt gt 运算符 如下所示 test gt Seq iter any to string gt gt printfn line s gt gt 运算符在这种情况下做什么 序列中的每个项目 在本例中为数组 是否被
  • .NET 异步流读/写

    我一直在尝试解决这个 并发编程 考试练习 C 知道Stream类包含int Read byte buffer int offset int size and void Write byte buffer int offset int siz