连续打印大量字符时.net 控制台应用程序停止响应

发布于 2024-07-20 22:29:23 字数 538 浏览 6 评论 0原文

我试图弄清楚为什么当我要求我正在开发的程序向其运行的控制台输出大量字符时,它会进入“无响应”模式。

我尝试创建一个仅打印的小示例字符,并且在大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风铃鹿 2024-07-27 22:29:23

当您将其转换为角色时,您还会将控制字符发送到控制台以获得一些较低的 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.

找回味觉 2024-07-27 22:29:23

好吧,它会吐出很多废话(并且会发出很多嘟嘟声,除非你屏蔽掉字符 7,这是一个铃),但它对我来说永远不会变得没有反应。

这将取决于您的控制台如何处理控制字符 - 您使用的是哪个控制台,在哪个操作系统上以及使用哪种语言?

此外,为什么您想要将不可打印的字符发送到控制台? 如果您将循环保留为 ASCII (32-126),会发生什么? 例如:

using System;

class Test
{   
    static void Main(string[] args)
    {
        int i=32;
        while (true)
        {
            Console.Write((char)i);
            i++;
            if (i == 127)
            {
                i = 32;
            }
        }
    }
}

这仍然表现出相同的行为吗?

您提到了调试器 - 如果您在调试器之外运行,您会得到相同的行为吗? (到目前为止我只从命令行进行了测试。)

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:

using System;

class Test
{   
    static void Main(string[] args)
    {
        int i=32;
        while (true)
        {
            Console.Write((char)i);
            i++;
            if (i == 127)
            {
                i = 32;
            }
        }
    }
}

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.)

温柔戏命师 2024-07-27 22:29:23

顺便说一句,您可以省略 i<255 并简单地编写:
for (int i = 0; ;i = (i+1) % 255 )

或者按照乔恩的答案,你可以像这样简化

using System;

class Test
{   
    static void Main(string[] args)
    {

        for(var i=0;;i=(i+1) % 126)
        {
            Console.Write((char)(i+32));
        }
    }
}

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

using System;

class Test
{   
    static void Main(string[] args)
    {

        for(var i=0;;i=(i+1) % 126)
        {
            Console.Write((char)(i+32));
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文