连续打印大量字符时.net 控制台应用程序停止响应
我试图弄清楚为什么当我要求我正在开发的程序向其运行的控制台输出大量字符时,它会进入“无响应”模式。
我尝试创建一个仅打印的小示例字符,并且在大约 10-20 秒后,这确实也会对我“无响应”:
static void Main(string[] args)
{
for (int i = 0; i < 255; i = (i+1) % 255)
{
Console.Write(((char)i));
}
}
程序仍在运行,即使控制台窗口“无响应”,我仍然可以暂停调试器并继续它,但是控制台窗口已损坏。
问题是,控制台不介意吐出无数的整数:
static void Main(string[] args)
{
for (int i = 0; i < 255; i = (i+1) % 255)
{
Console.Write(i);
}
}
任何想法都非常感激。 谢谢!
I am trying to figure out why a program I am working on goes in to "not responding" mode when I ask it to output a large amount of characters to the console it is running in.
I tried creating a small example that just prints out characters, and this will indeed also go "not responding" on me after some 10-20 seconds:
static void Main(string[] args)
{
for (int i = 0; i < 255; i = (i+1) % 255)
{
Console.Write(((char)i));
}
}
The program is still running though, even though the console window is "not responding", I can still pause the debugger and continue it, but the console window is broken.
The thing is, the console do not mind spitting out an endless amount of integers:
static void Main(string[] args)
{
for (int i = 0; i < 255; i = (i+1) % 255)
{
Console.Write(i);
}
}
Any ideas is much appreaciated. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您将其转换为角色时,您还会将控制字符发送到控制台以获得一些较低的
i
值。 我猜想这与重复输出其中一些控制字符有关。When you cast it to a character, you're also sending control characters to the console for some lower values of
i
. I'd guess is has something to do with outputting some of those control characters repeatedly.好吧,它会吐出很多废话(并且会发出很多嘟嘟声,除非你屏蔽掉字符 7,这是一个铃),但它对我来说永远不会变得没有反应。
这将取决于您的控制台如何处理控制字符 - 您使用的是哪个控制台,在哪个操作系统上以及使用哪种语言?
此外,为什么您想要将不可打印的字符发送到控制台? 如果您将循环保留为 ASCII (32-126),会发生什么? 例如:
这仍然表现出相同的行为吗?
您提到了调试器 - 如果您在调试器之外运行,您会得到相同的行为吗? (到目前为止我只从命令行进行了测试。)
Well it will spew out a lot of nonsense (and beep a lot, unless you mask out character 7, which is a bell) but it never becomes unresponsive for me.
It will depend on how your console handles control characters though - which console are you using, on which operating system and with which language?
Moreover, why do you want to send unprintable characters to the console? If you keep your loop to ASCII (32-126) what happens? For example:
Does that still exhibit the same behaviour?
You mention the debugger - do you get the same behaviour if you run outside the debugger? (I've only tested from the command line so far.)
顺便说一句,您可以省略
i<255
并简单地编写:for (int i = 0; ;i = (i+1) % 255 )
或者按照乔恩的答案,你可以像这样简化
Just as an aside, you can omit
i<255
and simply write:for (int i = 0; ;i = (i+1) % 255 )
or to go with Jon's answer you can simplify that like this