缓存 System.Console 输出

发布于 2024-08-12 06:55:31 字数 1544 浏览 2 评论 0原文

我们有一些方法可以向控制台输出大量彩色文本。

它实际上是一个通过循环数据集合构建的菜单。

因为我们是逐项写入的(一行包含许多不同颜色的项), 这个过程在低端和中端机器上相当慢。在这些机器上,您可以看到每一行都被一行一行地写入,这对于最终用户来说是相当令人沮丧的。

这个菜单必须显示很多次,所以我想知道我是否不能以某种方式“缓存”这个输出。如果没有彩色文本,我只需将输出存储在字符串中并一次性写入,但我不想丢失颜色信息。

有什么建议吗?

更新:只是为了了解输出的强度: 更新2:更新了草率的代码,但仍然想以某种方式缓冲输出。 MoveBufferArea 被证明是一个部分解决方案(它创建了不需要的滚动条)

DefaultBanner();

WriteLine("Available tests:\n", ConsoleColor.White);

var methodNames = methods.Select(m => ((TestAttribute)m.GetCustomAttributes(false)[0]).Name).ToArray();

int vertical = 0;
for (int i = 1; i <= methods.Length; i++)
{
    if (i > methods.Length / 2)
    {
        Console.SetCursorPosition(40, 4 + vertical++);
    }

    Write("(");
    Write(i, ConsoleColor.Yellow);
    WriteLine(") {0:00}", methodNames[i - 1]);
}

Write("\n(");
Write(items.Count + 1, ConsoleColor.Yellow);
Write(") Set address | (");
Write(items.Count + 2, ConsoleColor.Yellow);
Write(") View Log | (");
Write(items.Count + 3, ConsoleColor.Yellow);
Write(") Open Log directory \n(");
Write(items.Count + 4, ConsoleColor.Yellow);
Write(") Open configuration | (");
Write(items.Count + 5, ConsoleColor.Yellow);
Write(") View current configuration | (");
Write(items.Count + 6, ConsoleColor.Yellow);
WriteLine(") Quit");
Write("\nYour selection: ");

int command = 0;
while (!ConsoleReader<int>.TryReadValueInRange(1, items.Count + 6,out command));
return command;

Write 方法只是封装一些行为的辅助方法 System.Console 类的,这样我们就不必继续设置颜色。

We have some methods that output a lot of coloured text to the Console.

It's actually a menu that is built by looping through a collection of data.

Because we are writing item by item (and one line contains many items in different colours),
this process is rather slow on low-end and mid-end machines. On those machines you can see each line being written one by one and it's rather frustrating for the end user.

This menu has to be displayed many times so I'm wondering if I can't just "cache" this output somehow. If there was no coloured text I'd simply store the output in a string and write it in one go, but I don't want to lose colour information.

Any suggestions?

Update: Just to give an idea of how intensive the output is:
Update 2: Updated the sloppy code, but still want to buffer the output somehow. MoveBufferArea proves to be a partial solution (it creates unwanted scrollbars)

DefaultBanner();

WriteLine("Available tests:\n", ConsoleColor.White);

var methodNames = methods.Select(m => ((TestAttribute)m.GetCustomAttributes(false)[0]).Name).ToArray();

int vertical = 0;
for (int i = 1; i <= methods.Length; i++)
{
    if (i > methods.Length / 2)
    {
        Console.SetCursorPosition(40, 4 + vertical++);
    }

    Write("(");
    Write(i, ConsoleColor.Yellow);
    WriteLine(") {0:00}", methodNames[i - 1]);
}

Write("\n(");
Write(items.Count + 1, ConsoleColor.Yellow);
Write(") Set address | (");
Write(items.Count + 2, ConsoleColor.Yellow);
Write(") View Log | (");
Write(items.Count + 3, ConsoleColor.Yellow);
Write(") Open Log directory \n(");
Write(items.Count + 4, ConsoleColor.Yellow);
Write(") Open configuration | (");
Write(items.Count + 5, ConsoleColor.Yellow);
Write(") View current configuration | (");
Write(items.Count + 6, ConsoleColor.Yellow);
WriteLine(") Quit");
Write("\nYour selection: ");

int command = 0;
while (!ConsoleReader<int>.TryReadValueInRange(1, items.Count + 6,out command));
return command;

The Write methods are simply helper methods that encapsulate some behaviour
of the System.Console class so we wouldn't have to keep setting the colour.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(5

七月上 2024-08-19 06:55:31

也许您可以使用 MoveBufferArea 函数来实现一些巧妙的操作。如果您有一个当前对用户不可见的缓冲区区域,该怎么办?您可以在那里创建缓冲区\菜单,然后一次性将整个区域复制到可见缓冲区的一部分。我不知道您对缓冲区的可见/不可见区域有多少控制权,如果您可以像这样使用它们,这只是一个建议。

我能想到的唯一其他选择是创建自己的模仿控制台的控件。然后,您可以允许缓冲读/写,或者仅在进行多项更改后刷新。但这可能会带来很多额外的工作。

Maybe you could something clever using the MoveBufferArea function. What about if you had a area of the buffer that was not currently visible to the user. You could create your buffer\menu there and then copy the whole region to a part of the visible buffer in one go. I don't know how much control you have over the visible/non-visible areas of the buffer and if you can use them like this, it's just a suggestion.

The only other option I can think of is creating your own control that mimics the console. You could then make allow buffered reads/writes or only refresh after you've made several changes. But this could be a lot of extra work.

灵芸 2024-08-19 06:55:31

有没有办法将属性评估逻辑移至“循环之外”?我怀疑这不是你的代码中最快的部分。

Is there a way to move your attribute-evaluating logic "outside the loop?" I suspect it's not the quickest part of your code.

俯瞰星空 2024-08-19 06:55:31

我不知道您的项目是否可行,但一种选择是使用某种类型的标记语言将颜色存储在字符串本身内,例如 Text in Yellow(或者你可以使用普通的 CSS 选择器,或者其他什么)。这样您就可以使用纯字符串缓存。

顺便请记住,.NET Framework 已经实现了这样一个缓存:请参阅 String.InternString.IsInterned< /a>.

I don't know if it is possible for your project, but one option is using some type of markup language to store the color inside the string itself, for example <yellow>Text in yellow</yellow> (or your could use plain CSS selectors, or whatever). This way you could use a plain string cache.

By the way remember that the .NET Framework already implements one such cache: see String.Intern and String.IsInterned.

未央 2024-08-19 06:55:31

请记住,控制台是一个可配置的设备,这意味着您可以编写自己的设备(尽管我不推荐它)。有一种方法可以在配置文件中指定哪个类进行跟踪以及哪个类进行调试输出。

Keep in mind that Console is a configurable device, meaning that you can write your own (though I don't recommend it). There is a way to specify in config file which class does Tracing and which class does Debug output.

鸢与 2024-08-19 06:55:31

有没有办法要求您的客户确保他们正在运行硬件加速图形(检查驱动程序)?如果它们使用仅限 VGA 的默认 Microsoft 驱动程序运行,则控制台速度将极其缓慢。

Is there a way to ask your clients to make sure they are running hardware-accelerated graphics (check the drivers)? If they are running with VGA-Only default Microsoft driver, then console will be painfully slow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文