C# Stream.Read 超时 [英] C# Stream.Read with timeout

查看:58
本文介绍了C# Stream.Read 超时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个流媒体阅读器:

I have this streamreader:

            Boolean read = false;
            while (wline!="exit")
            {

                while (!read || streamReader.Peek() >= 0)
                {
                    read = true;
                    Console.Write((char)streamReader.Read());
                }
                wline = Console.ReadLine();
                streamWriter.Write(wline+"
");
                streamWriter.Flush();

            }

如何为 Read() 方法设置超时时间?谢谢

How to set a timeout for Read() method? thanks

推荐答案

如果这是System.IO.StreamReader,则在BaseStream上设置:

If this is System.IO.StreamReader, then set it on the BaseStream:

streamReader.BaseStream.ReadTimeout = 2000;  //milliseconds, so 2 seconds

这篇关于C# Stream.Read 超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆