使 .NET TextBox 工作 FIFO 样式 [英] making a .NET TextBox work FIFO style

查看:19
本文介绍了使 .NET TextBox 工作 FIFO 样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用 C# 编写的 win GUI 应用程序,其中有一个 TextBox 组件,我将日志写入其中.在某些时候,它变得太加载了,整个应用程序开始动摇.我想制定一种有效的机制来使其成为 FIFO,这意味着 - 使其成为固定大小并自动删除最旧的内容.

I have a win GUI application written in C#, in which I have a TextBox component that I write my log to. at some point it gets too loaded and the entire app is starting to falter. I'd like to make an efficient mechanism to make it a FIFO, meaning - make it a fixed size and deleting the oldest content automatically.

是否有任何 .net/c# 功能可以做到这一点?否则,这样做的正确方法是什么?

is there any .net/c# feature for doing that? otherwise, what will be the right way to do this?

UPDATE:我对其他类型的文本数据也有这个问题,而不仅仅是日志.因此,ListBox 对我来说不是一个合适的解决方案.

UPDATE: I also have this issue with other sort of textual data, and not only with logs. therefor, the ListBox is not a proper solution for me.

推荐答案

要为文本创建循环缓冲区,我会使用 StringBuilder,其容量设置为数据量的大约两倍我要展示.

To create a circular buffer for text, I'd use a StringBuilder, with the capacity set to about twice the amount of data I want to display.

const int DisplaySize = 10000;
StringBuilder fifo = new StringBuilder(2 * DisplaySize);

string AppendToFifo( string s )
{
    if (s.Length >= DisplaySize) {
        // FACT: the display will only include data from s
        // therefore, toss the entire buffer, and only keep the tail of s
        fifo.Clear();
        fifo.Append(s, s.Length - DisplaySize, DisplaySize);
        return fifo.ToString();
    }
    if (fifo.Length + s.Length > fifo.Capacity) {
        // FACT: we will overflow the fifo
        // therefore, keep only data in the fifo that remains on the display
        fifo.Remove(0, fifo.Length + s.Length - DisplaySize);
    }
    fifo.Append(s);
    if (fifo.Length <= DisplaySize) {
        // FACT: the entire fifo content fits on the display
        // therefore, send it all
        return fifo.ToString();
    }
    // FACT: the fifo content exceed the display size
    // therefore, extract just the tail
    return fifo.ToString(fifo.Length - DisplaySize, DisplaySize);
}

快速路径,当所有 if 条件都不为真时,会避免所有不必要的副本(在字符串不可变的 .NET 世界中,无法避免创建输出字符串的最终副本).在其他情况下,只复制需要的字符.增加缓冲区的容量将提高快速路径的利用率.我一直小心避免做的是使用保留在显示器上的旧内容创建一个字符串对象,除了与新内容连接之外没有任何用处,并立即变成垃圾.

The fast path, when none of the if conditions are true, avoids all unnecessary copies (in the .NET world where strings are immutable, the final copy to create the output string cannot be avoided). And in the other cases only needed characters are copied. Increasing the capacity of the buffer will improve utilization fraction of the fast path. What I've been careful to avoid doing is creating a string object with the old content that remains on the display, which has no purpose except to concatenate with the new content, and immediately becomes garbage.

显然,如果你使用 p/invoke 传递一个指向 StringBuffer 内容的指针,而不是复制一个子字符串,那会更有效率.

Obviously, if you use p/invoke to pass a pointer to the StringBuffer's content instead of copying out a substring, that will be even more efficient.

这篇关于使 .NET TextBox 工作 FIFO 样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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