.net console application stops responding when printing a large number of characters in a row

I’m trying to understand why the program I’m working on goes into non-responding mode when I ask him to output a large number of characters to the console in which he works.

I tried to create a small example that just prints characters, and it really will also β€œnot respond” to me in 10-20 seconds:

static void Main(string[] args) { for (int i = 0; i < 255; i = (i+1) % 255) { Console.Write(((char)i)); } } 

The program still works, although although the console window is "not responding", I can still pause the debugger and continue it, but the console window is broken.

The fact is that the console does not mind splashing out an infinite number of integers:

 static void Main(string[] args) { for (int i = 0; i < 255; i = (i+1) % 255) { Console.Write(i); } } 

Any ideas are very receptive. Thanks!

+2
source share
3 answers

Well, that will break a lot of nonsense (and hear a lot if you do not mask the symbol 7, which is a bell), but it never ceases to answer for me.

It will depend on how your console handles control characters, though - which console do you use, on which operating system and in which language?

Also, why do you want to send non-printable characters to the console? If you save your loop in ASCII (32-126), what will happen? For instance:

 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 the same behavior persist?

You mentioned the debugger β€” do you get the same behavior if you go beyond the debugger? (I only tested from the command line so far.)

+4
source

When you pass it to a character, you also send control characters to the console for some lower i values. I would suggest that this has anything to do with the output of some of these control characters.

+5
source

As an aside, you can omit i<255 and simply write: for (int i = 0; ;i = (i+1) % 255 )

or, to go with John, you can simplify it, like this.

 using System; class Test { static void Main(string[] args) { for(var i=0;;i=(i+1) % 126) { Console.Write((char)(i+32)); } } } 
+1
source

Source: https://habr.com/ru/post/1435936/


All Articles