Skip to content

Commit 2890d50

Browse files
committed
fix: 防止阻塞的方式不停更新ui
1 parent e539bbb commit 2890d50

File tree

3 files changed

+83
-21
lines changed

3 files changed

+83
-21
lines changed

llcomNext/LLCOM/Models/TerminalObject.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ private void TerminalChanged()
4242

4343
//可视范围内的宽高
4444
private int _windowWidth;
45+
public int WindowWidth => _windowWidth;
4546
private int _windowHeight;
47+
public int WindowHeight => _windowHeight;
4648

4749
//添加新的一行上去
4850
private void AddLine()
@@ -262,6 +264,26 @@ public void ChangePosition(int x, int y)
262264
PositionX = x;
263265
PositionY = y;
264266
}
267+
268+
//TODO)) 仅用于测试
269+
public void ChangeStyle(
270+
int? foreground = null,
271+
int? background = null,
272+
bool? isBold = null,
273+
bool? isItalic = null,
274+
bool? isUnderLine = null)
275+
{
276+
if (foreground != null)
277+
CurrentState.Foreground = foreground.Value;
278+
if (background != null)
279+
CurrentState.Background = background.Value;
280+
if (isBold != null)
281+
CurrentState.IsBold = isBold.Value;
282+
if (isItalic != null)
283+
CurrentState.IsItalic = isItalic.Value;
284+
if (isUnderLine != null)
285+
CurrentState.IsUnderLine = isUnderLine.Value;
286+
}
265287

266288
/// <summary>
267289
/// 获取可以显示的行数据

llcomNext/LLCOM/ViewModels/DataViews/TerminalViewModel.cs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System.Collections.Generic;
33
using System.Collections.ObjectModel;
44
using System.Text;
5+
using System.Threading.Tasks;
56
using CommunityToolkit.Mvvm.ComponentModel;
67
using CommunityToolkit.Mvvm.Input;
78
using LLCOM.Models;
@@ -30,13 +31,18 @@ public TerminalViewModel(Func<Type, ViewModelBase> getService)
3031
}
3132

3233
[RelayCommand]
33-
private void Test()
34+
private async Task Test()
3435
{
35-
TerminalObject.AddText($"中文测试abcdefghijklmnopqrstuvwxyz".ToCharArray());
36-
TerminalObject.ChangePosition(1, 0);
37-
TerminalObject.AddText($"12345".ToCharArray());
38-
39-
TerminalChangedEvent?.Invoke(this, TerminalObject.GetShowLines());
36+
var random = new Random();
37+
var testChars = "测试Test".ToCharArray();
38+
for(int i=0; i<1000; i++)
39+
{
40+
TerminalObject.ChangeStyle(random.Next(30,38),random.Next(30,38));
41+
TerminalObject.ChangePosition(random.Next(0,TerminalObject.WindowWidth), random.Next(0,TerminalObject.WindowHeight));
42+
TerminalObject.AddText([testChars[random.Next(0, testChars.Length)]]);
43+
TerminalChangedEvent?.Invoke(this, TerminalObject.GetShowLines());
44+
await Task.Delay(1);
45+
}
4046
}
4147

4248
//终端对象, TODO)) 后续需要为每项操作加锁

llcomNext/LLCOM/Views/DataViews/TerminalView.axaml.cs

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,10 @@ namespace LLCOM.Views;
2020

2121
public partial class TerminalView : UserControl
2222
{
23+
private DispatcherTimer _updateTimer;
24+
private bool _dataChanged = false;
25+
readonly List<List<TerminalBlock>> _terminalBlocks = [];
26+
2327
public TerminalView()
2428
{
2529
InitializeComponent();
@@ -34,11 +38,49 @@ protected override void OnUnloaded(RoutedEventArgs e)
3438
MainArea.PropertyChanged -= MainArea_PropertyChanged;
3539
Utils.Setting.TerminalChangedEvent -= TerminalChangedEvent;
3640
((TerminalViewModel)DataContext!).TerminalChangedEvent -= TerminalChangedEvent;
41+
_updateTimer.Stop();
3742
Debug.WriteLine("TerminalView unloaded.");
3843
}
3944

4045
private void Control_OnLoaded(object? sender, RoutedEventArgs e)
4146
{
47+
//搞个定时器来更新UI,防止阻塞
48+
_updateTimer = new DispatcherTimer
49+
{
50+
Interval = TimeSpan.FromMilliseconds(100)
51+
};
52+
_updateTimer.Tick += (s, e) =>
53+
{
54+
if (_dataChanged)
55+
{
56+
lock (_terminalBlocks)
57+
{
58+
// 执行更新逻辑
59+
//清空文本
60+
MainTextBlock.Inlines!.Clear();
61+
foreach (var line in _terminalBlocks)
62+
{
63+
foreach (var block in line)
64+
{
65+
//添加块
66+
var run = new Run(block.Text)
67+
{
68+
[!Span.ForegroundProperty] = new Binding(block.ForegroundBindingName) { Source = Utils.Setting },
69+
[!Span.BackgroundProperty] = new Binding(block.BackgroundBindingName) { Source = Utils.Setting },
70+
FontWeight = block.IsBold ? FontWeight.Bold : FontWeight.Normal,
71+
FontStyle = block.IsItalic ? FontStyle.Italic : FontStyle.Normal,
72+
TextDecorations = block.IsUnderLine ? TextDecorations.Underline : null,
73+
};
74+
MainTextBlock.Inlines.Add(run);
75+
}
76+
MainTextBlock.Inlines.Add(new LineBreak());
77+
}
78+
}
79+
_dataChanged = false; // 重置标记
80+
}
81+
};
82+
_updateTimer.Start();
83+
4284
Utils.Setting.TerminalChangedEvent += TerminalChangedEvent;
4385
((TerminalViewModel)DataContext!).TerminalChangedEvent += TerminalChangedEvent;
4486
//加载完触发一次,顺便初始化窗口大小数据
@@ -80,27 +122,19 @@ private void MainScrollBar_OnScroll(object? sender, ScrollEventArgs e)
80122

81123
private void TerminalChangedEvent(object? sender, List<List<TerminalBlock>> e)
82124
{
83-
Dispatcher.UIThread.Invoke(() =>
125+
lock (_terminalBlocks)
84126
{
85-
//清空文本
86-
MainTextBlock.Inlines!.Clear();
127+
_terminalBlocks.Clear();
87128
foreach (var line in e)
88129
{
130+
var newLine = new List<TerminalBlock>();
89131
foreach (var block in line)
90132
{
91-
//添加块
92-
var run = new Run(block.Text)
93-
{
94-
[!Span.ForegroundProperty] = new Binding(block.ForegroundBindingName) { Source = Utils.Setting },
95-
[!Span.BackgroundProperty] = new Binding(block.BackgroundBindingName) { Source = Utils.Setting },
96-
FontWeight = block.IsBold ? FontWeight.Bold : FontWeight.Normal,
97-
FontStyle = block.IsItalic ? FontStyle.Italic : FontStyle.Normal,
98-
TextDecorations = block.IsUnderLine ? TextDecorations.Underline : null,
99-
};
100-
MainTextBlock.Inlines.Add(run);
133+
newLine.Add((TerminalBlock)block.Clone());
101134
}
102-
MainTextBlock.Inlines.Add(new LineBreak());
135+
_terminalBlocks.Add(newLine);
103136
}
104-
});
137+
}
138+
_dataChanged = true;
105139
}
106140
}

0 commit comments

Comments
 (0)