ReadLine问题 [英] ReadLine problem

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

问题描述

你好,


我在C#中遇到了ReadLine方法的问题。基本上我有一个连接到NetworkStream的StreamReader(读取)。这个部分没问题。


在阅读时我使用以下几行:


string file ="" ;;

字符串行;

while((line = read.ReadLine())!= null)

{

file + = " \\ nn" + line;

}


当我调试时,代码会一直读到网络流中的最后一行。当它试图读取最后一行时,调试失败,有趣的是,窗体的窗口(上面给出的行在Form1类中)出现。按F10,F11或F5不起作用。实际上它们在Debug菜单中显示为灰色,但Stop Debug等仍然起作用。


我认为这是一个简单的问题,但我无法弄明白。


感谢您的帮助,


Halil

Hello,

I am having a trouble with the ReadLine method in C#. Basically I have a StreamReader (read) attached to a NetworkStream. No problem with this part.

While reading I use the lines below:

string file = "";
string line;
while ((line = read.ReadLine())!= null)
{
file += "\r\n"+line;
}

When I am debug, the code reads till the last line in the network stream. When it attempts to read the last line, debug fails, and interestingly, the window of the form (the lines given above are in the class Form1) appears. Pressing F10, F11 or F5 does not work. In fact they are greyed out in the Debug menu, but Stop Debug, etc still functions.

I think a simple problem but I could not figure it out.

Thank you for helping,

Halil

推荐答案

1.)我希望编译器能够发牢骚并说该行可能尚未初始化(或类似)。

2.)在循环中连接字符串时最好使用StringBuilder。它更有效。

3.)运行没有任何断点的代码会发生什么?
1.) I would expect the compiler to whine and say that line may not have been initialized (or similar).
2.) Better use StringBuilder when concatenating strings in a loop. It''s more efficient.
3.) What happens when you run the code without any break points?


我猜测调试没有被破坏,而是你没有使用readline()正确。

ReadLine()是一个阻塞调用,只要它们不是流的结束,它就不会出现在仍然是已连接/打开的网络流。

因此,您的调试将停留在那里等待ReadLine()命令。

如果您间隔开你的循环机制使它长篇大论,你将能够逐步执行并看到它位于ReadLine()语句中。

你需要编码另外采取预防措施。
I would guess that the debug is not broken, but rather that you are not using readline() correct.
ReadLine() is a blocking call so long as their is no "end of stream", which there wouldn''t be on a network stream that is still "connected"/"open".
So your debug is stuck sitting there waiting at the ReadLine() command.
If you spaced out your looping mechanism to make it "long winded", you would be able to do step-by-step execution and see that it sits there on the ReadLine() statement.
You will need to code up additional precautions against this.



..

ReadLine()是一个阻止通话,只要他们的不是流的结束,它不会在网络流上仍然是& 已连接/打开。

...
..
ReadLine() is a blocking call so long as their is no "end of stream", which there wouldn''t be on a network stream that is still "connected"/"open".
...



完全错过了一个。

Missed that one completely.


这篇关于ReadLine问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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