22using System . Collections . Generic ;
33using System . Diagnostics ;
44using System . Text ;
5+ using System . Threading ;
56using Avalonia ;
67using Avalonia . Controls ;
78using Avalonia . Controls . Documents ;
@@ -20,9 +21,9 @@ namespace LLCOM.Views;
2021
2122public partial class TerminalView : UserControl
2223{
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 = [ ] ;
2627
2728 public TerminalView ( )
2829 {
@@ -38,22 +39,23 @@ protected override void OnUnloaded(RoutedEventArgs e)
3839 MainArea . PropertyChanged -= MainArea_PropertyChanged ;
3940 Utils . Setting . TerminalChangedEvent -= TerminalChangedEvent ;
4041 ( ( TerminalViewModel ) DataContext ! ) . TerminalChangedEvent -= TerminalChangedEvent ;
41- _updateTimer . Stop ( ) ;
42+ _cts . Cancel ( ) ;
43+ _dataChangeWaitHandle . Set ( ) ;
4244 Debug . WriteLine ( "TerminalView unloaded." ) ;
4345 }
4446
4547 private void Control_OnLoaded ( object ? sender , RoutedEventArgs e )
4648 {
47- //搞个定时器来更新UI ,防止阻塞
48- _updateTimer = new DispatcherTimer
49+ //搞个单独线程来更新UI ,防止阻塞
50+ new Thread ( ( ) =>
4951 {
50- Interval = TimeSpan . FromMilliseconds ( 100 )
51- } ;
52- _updateTimer . Tick += ( s , e ) =>
53- {
54- if ( _dataChanged )
52+ while ( _cts . Token . IsCancellationRequested == false )
5553 {
56- _dataChanged = false ; // 重置标记
54+ _dataChangeWaitHandle . WaitOne ( ) ;
55+ _dataChangeWaitHandle . Reset ( ) ;
56+ if ( _cts . Token . IsCancellationRequested )
57+ break ;
58+
5759 var tempLines = new List < List < TerminalBlock > > ( ) ;
5860 lock ( _terminalBlocks )
5961 {
@@ -62,28 +64,31 @@ private void Control_OnLoaded(object? sender, RoutedEventArgs e)
6264 }
6365
6466 // 执行更新逻辑
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 )
7072 {
71- //添加块
72- var run = new Run ( block . Text )
73+ foreach ( var block in line )
7374 {
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 ( ) ) ;
8187 }
82- MainTextBlock . Inlines . Add ( new LineBreak ( ) ) ;
83- }
88+ } ) ;
89+ Thread . Sleep ( 150 ) ;
8490 }
85- } ;
86- _updateTimer . Start ( ) ;
91+ } ) . Start ( ) ;
8792
8893 Utils . Setting . TerminalChangedEvent += TerminalChangedEvent ;
8994 ( ( TerminalViewModel ) DataContext ! ) . TerminalChangedEvent += TerminalChangedEvent ;
@@ -132,7 +137,7 @@ private void TerminalChangedEvent(object? sender, List<List<TerminalBlock>> e)
132137 //克隆数据
133138 _terminalBlocks . AddRange ( CloneAllLines ( e ) ) ;
134139 }
135- _dataChanged = true ;
140+ _dataChangeWaitHandle . Set ( ) ;
136141 }
137142
138143 private List < List < TerminalBlock > > CloneAllLines ( List < List < TerminalBlock > > lines )
0 commit comments