gpt4 book ai didi

c# - 当太快和太多数据需要更新时,UI 无响应

转载 作者:行者123 更新时间:2023-11-30 19:20:37 25 4
gpt4 key购买 nike

我做了一个控件来记录来自不同线程的消息到屏幕。它使用富文本框显示格式化文本。

当有 20 个线程每 200-250 毫秒附加一次它们的消息时,主 UI 会在一段时间内变得无响应,并且在处理完等待的消息后,UI 会再次开始响应。线程运行时窗口移动不流畅。

富文本框的消息写入与锁同步。

您有什么建议可以提高性能?我计划运行 100 个线程。


这是我的代码。我将控制台输出重定向到它,它记录所有正在发生的事情,并以格式化的形式显示在富文本框中。

public void RedirectStandardOutput()
{
Console.SetOut(ConsoleStream);

System.Diagnostics.Debug.Listeners.Add(new System.Diagnostics.TextWriterTraceListener(Console.Out));
System.Diagnostics.Debug.AutoFlush = true;
}

控制台被重定向后所有 Console.WriteLine("bla bla");被写入屏幕。


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CoreLib.Parsers;

namespace ConsoleWidget
{
public class ConsoleStream : System.IO.TextWriter
{
private readonly object _textBoxLock = new object();

public RichTextBox TextBox { get; set; }
public List<TextFormat> TextFormatList { get; set; }

public bool AutoClear { get; set; }
public int AutoClearLength { get; set; }
public bool AutoSave { get; set; }
public string AutoSaveDir { get; set; }


public ConsoleStream()
{
TextFormatList = new List<TextFormat>();
}

public void AppendText(string text)
{
if (TextBox == null) return;

var textLength = TextBox.TextLength;

if (AutoClear && textLength > AutoClearLength)
{
if (AutoSave)
{
var dir = string.Format(@"{0}\{1}\{2}", Environment.CurrentDirectory, AutoSaveDir, CoreLib.Extensions.DateTimeExtensions.DateTimeNowDir);

if (!System.IO.Directory.Exists(dir))
System.IO.Directory.CreateDirectory(dir);

var path = string.Format(@"{0}\{1}.log", dir, CoreLib.Extensions.DateTimeExtensions.GetDateTimeNowFileName);

TextBox.SaveFile(path);
}

TextBox.Clear();
}

TextBox.AppendText(text);

// Format text.
foreach (var textFormat in TextFormatList)
{
int beginIndex;
int length;

if (textFormat.GetFormatProperties(text, out beginIndex, out length))
{
// RichTextBox counts newline "\r\n" which is double char as single char.
// Causes shifting in selection starts. The lines below count the "\r" chars before the beginIndex.
var leftText = text.Substring(0, beginIndex);
var newLineCount = leftText.Count(c => c == '\r');

TextBox.SelectionStart = textLength + beginIndex - newLineCount;
TextBox.SelectionLength = length;
if (!textFormat.Color.IsEmpty)
TextBox.SelectionColor = textFormat.Color;
if (textFormat.Font != null)
TextBox.SelectionFont = textFormat.Font;
}
}

TextBox.ScrollToCaret();
}

public void Clear()
{
lock (_textBoxLock)
{
TextBox.Clear();
}
}

public int TextLength
{
get
{
lock (_textBoxLock)
{
return TextBox.TextLength;
}
}
}

public void SaveAs(string path)
{
lock (_textBoxLock)
{
TextBox.SaveFile(path);
}
}

public override Encoding Encoding
{
get { return Encoding.Default; }
}

public override void Write(string value)
{
if (TextBox == null) return;

var action = (Action)(() => AppendText(value));

lock (_textBoxLock)
{
if (TextBox.InvokeRequired)
TextBox.BeginInvoke(action);
else
action();
}
}

public override void WriteLine()
{
Write(NewLine);
}

public override void WriteLine(string value)
{
Write(value);
WriteLine();
}
}
}

最佳答案

让他们写入一个缓冲区 RichTextBox(一个实际上不是您表单的一部分的缓冲区),并且每隔 250 毫秒左右将缓冲区附加到 UI RichTextBox。

关于c# - 当太快和太多数据需要更新时,UI 无响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5678022/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com