2
2
using System . Collections . Generic ;
3
3
using System . Diagnostics ;
4
4
using System . Text ;
5
+ using System . Threading ;
5
6
using Avalonia ;
6
7
using Avalonia . Controls ;
7
8
using Avalonia . Controls . Documents ;
@@ -20,9 +21,9 @@ namespace LLCOM.Views;
20
21
21
22
public partial class TerminalView : UserControl
22
23
{
23
- private DispatcherTimer _updateTimer ;
24
- private bool _dataChanged = false ;
25
- readonly List < List < TerminalBlock > > _terminalBlocks = [ ] ;
24
+ private readonly CancellationTokenSource _cts = new CancellationTokenSource ( ) ;
25
+ private readonly EventWaitHandle _dataChangeWaitHandle = new EventWaitHandle ( false , EventResetMode . ManualReset ) ;
26
+ private readonly List < List < TerminalBlock > > _terminalBlocks = [ ] ;
26
27
27
28
public TerminalView ( )
28
29
{
@@ -38,22 +39,23 @@ protected override void OnUnloaded(RoutedEventArgs e)
38
39
MainArea . PropertyChanged -= MainArea_PropertyChanged ;
39
40
Utils . Setting . TerminalChangedEvent -= TerminalChangedEvent ;
40
41
( ( TerminalViewModel ) DataContext ! ) . TerminalChangedEvent -= TerminalChangedEvent ;
41
- _updateTimer . Stop ( ) ;
42
+ _cts . Cancel ( ) ;
43
+ _dataChangeWaitHandle . Set ( ) ;
42
44
Debug . WriteLine ( "TerminalView unloaded." ) ;
43
45
}
44
46
45
47
private void Control_OnLoaded ( object ? sender , RoutedEventArgs e )
46
48
{
47
- //搞个定时器来更新UI ,防止阻塞
48
- _updateTimer = new DispatcherTimer
49
+ //搞个单独线程来更新UI ,防止阻塞
50
+ new Thread ( ( ) =>
49
51
{
50
- Interval = TimeSpan . FromMilliseconds ( 100 )
51
- } ;
52
- _updateTimer . Tick += ( s , e ) =>
53
- {
54
- if ( _dataChanged )
52
+ while ( _cts . Token . IsCancellationRequested == false )
55
53
{
56
- _dataChanged = false ; // 重置标记
54
+ _dataChangeWaitHandle . WaitOne ( ) ;
55
+ _dataChangeWaitHandle . Reset ( ) ;
56
+ if ( _cts . Token . IsCancellationRequested )
57
+ break ;
58
+
57
59
var tempLines = new List < List < TerminalBlock > > ( ) ;
58
60
lock ( _terminalBlocks )
59
61
{
@@ -62,28 +64,31 @@ private void Control_OnLoaded(object? sender, RoutedEventArgs e)
62
64
}
63
65
64
66
// 执行更新逻辑
65
- //清空文本
66
- MainTextBlock . Inlines ! . Clear ( ) ;
67
- foreach ( var line in tempLines )
68
- {
69
- foreach ( var block in line )
67
+ Dispatcher . UIThread . Post ( ( ) =>
68
+ {
69
+ //清空文本
70
+ MainTextBlock . Inlines ! . Clear ( ) ;
71
+ foreach ( var line in tempLines )
70
72
{
71
- //添加块
72
- var run = new Run ( block . Text )
73
+ foreach ( var block in line )
73
74
{
74
- [ ! Span . ForegroundProperty ] = new Binding ( block . ForegroundBindingName ) { Source = Utils . Setting } ,
75
- [ ! Span . BackgroundProperty ] = new Binding ( block . BackgroundBindingName ) { Source = Utils . Setting } ,
76
- FontWeight = block . IsBold ? FontWeight . Bold : FontWeight . Normal ,
77
- FontStyle = block . IsItalic ? FontStyle . Italic : FontStyle . Normal ,
78
- TextDecorations = block . IsUnderLine ? TextDecorations . Underline : null ,
79
- } ;
80
- MainTextBlock . Inlines . Add ( run ) ;
75
+ //添加块
76
+ var run = new Run ( block . Text )
77
+ {
78
+ [ ! Span . ForegroundProperty ] = new Binding ( block . ForegroundBindingName ) { Source = Utils . Setting } ,
79
+ [ ! Span . BackgroundProperty ] = new Binding ( block . BackgroundBindingName ) { Source = Utils . Setting } ,
80
+ FontWeight = block . IsBold ? FontWeight . Bold : FontWeight . Normal ,
81
+ FontStyle = block . IsItalic ? FontStyle . Italic : FontStyle . Normal ,
82
+ TextDecorations = block . IsUnderLine ? TextDecorations . Underline : null ,
83
+ } ;
84
+ MainTextBlock . Inlines . Add ( run ) ;
85
+ }
86
+ MainTextBlock . Inlines . Add ( new LineBreak ( ) ) ;
81
87
}
82
- MainTextBlock . Inlines . Add ( new LineBreak ( ) ) ;
83
- }
88
+ } ) ;
89
+ Thread . Sleep ( 150 ) ;
84
90
}
85
- } ;
86
- _updateTimer . Start ( ) ;
91
+ } ) . Start ( ) ;
87
92
88
93
Utils . Setting . TerminalChangedEvent += TerminalChangedEvent ;
89
94
( ( TerminalViewModel ) DataContext ! ) . TerminalChangedEvent += TerminalChangedEvent ;
@@ -132,7 +137,7 @@ private void TerminalChangedEvent(object? sender, List<List<TerminalBlock>> e)
132
137
//克隆数据
133
138
_terminalBlocks . AddRange ( CloneAllLines ( e ) ) ;
134
139
}
135
- _dataChanged = true ;
140
+ _dataChangeWaitHandle . Set ( ) ;
136
141
}
137
142
138
143
private List < List < TerminalBlock > > CloneAllLines ( List < List < TerminalBlock > > lines )
0 commit comments