Skip to content

Commit 4d11623

Browse files
author
1loodFlame
committed
update: 优化lua日志打印数据量特大时卡住ui的问题
1 parent 4cd3d4c commit 4d11623

File tree

1 file changed

+69
-16
lines changed

1 file changed

+69
-16
lines changed

llcom/View/MainWindow.xaml.cs

Lines changed: 69 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@ private void Window_Loaded(object sender, RoutedEventArgs e)
198198
$"https://llcom.papapoi.com/tongji.html?{System.Reflection.Assembly.GetExecutingAssembly().GetName().Version}"
199199
);
200200

201+
new Thread(LuaLogPrintTask).Start();
202+
201203
//加载完了,可以允许点击
202204
MainGrid.IsEnabled = true;
203205

@@ -1001,32 +1003,81 @@ private bool luaLogPrintable
10011003
_luaLogPrintable = value;
10021004
}
10031005
}
1006+
1007+
//lua日志打印次数
10041008
private int luaLogCount = 0;
1009+
/// <summary>
1010+
/// 消息来的信号量
1011+
/// </summary>
1012+
private EventWaitHandle luaWaitQueue = new AutoResetEvent(false);
1013+
private List<string> luaLogsBuff = new List<string>();
10051014
private void LuaApis_PrintLuaLog(object sender, EventArgs e)
10061015
{
1007-
if (luaLogPrintable)
1016+
if(sender is string && sender != null)
1017+
{
1018+
lock(luaLogsBuff)
1019+
{
1020+
if (luaLogsBuff.Count > 500)
1021+
{
1022+
luaLogsBuff.Clear();
1023+
luaLogsBuff.Add("too many logs!");
1024+
//延时0.5秒,防止卡住ui线程
1025+
Thread.Sleep(500);
1026+
}
1027+
else
1028+
luaLogsBuff.Add(sender as string);
1029+
}
1030+
luaWaitQueue.Set();
1031+
}
1032+
}
1033+
1034+
private void LuaLogPrintTask()
1035+
{
1036+
luaWaitQueue.Reset();
1037+
Tools.Global.ProgramClosedEvent += (_, _) =>
1038+
{
1039+
luaWaitQueue.Set();
1040+
};
1041+
while (true)
10081042
{
1043+
luaWaitQueue.WaitOne();
1044+
if (Tools.Global.isMainWindowsClosed)
1045+
return;
1046+
var logsb = new StringBuilder();
1047+
lock (luaLogsBuff)
1048+
{
1049+
for(int i=0;i<luaLogsBuff.Count;i++)
1050+
logsb.AppendLine(luaLogsBuff[i]);
1051+
luaLogsBuff.Clear();
1052+
}
1053+
1054+
if (!luaLogPrintable)
1055+
continue;
1056+
if (logsb.Length == 0)
1057+
continue;
10091058
luaLogCount++;
1010-
//新起一个线程,绕过线程锁卡死问题
1011-
Task.Run(() =>
1059+
var logs = logsb.ToString();
1060+
this.Dispatcher.Invoke(()=>
10121061
{
1013-
this.Dispatcher.Invoke(new Action(delegate
1062+
luaLogTextBox.IsEnabled = false;//确保文字不再被选中,防止wpf卡死
1063+
if (luaLogCount >= 1000)
10141064
{
1015-
luaLogTextBox.IsEnabled = false;//确保文字不再被选中,防止wpf卡死
1016-
if (luaLogCount >= 1000)
1017-
{
1018-
luaLogTextBox.Clear();
1019-
luaLogTextBox.AppendText("Lua log too long, auto clear.\r\n" +
1020-
"more logs see lua log file.\r\n");
1021-
}
1022-
luaLogTextBox.AppendText((sender as string) + "\r\n");
1023-
luaLogTextBox.ScrollToEnd();
1024-
if(!luaLogTextBox.IsMouseOver)
1025-
luaLogTextBox.IsEnabled = true;
1026-
}));
1065+
luaLogTextBox.Clear();
1066+
luaLogTextBox.AppendText("Lua log too long, auto clear.\r\n" +
1067+
"more logs see lua log file.\r\n");
1068+
luaLogCount = 0;
1069+
}
1070+
luaLogTextBox.AppendText(logs);
1071+
luaLogTextBox.ScrollToEnd();
1072+
if (!luaLogTextBox.IsMouseOver)
1073+
luaLogTextBox.IsEnabled = true;
10271074
});
1075+
//正常就延时10ms,防止卡住ui线程
1076+
Thread.Sleep(10);
10281077
}
10291078
}
1079+
1080+
10301081
private void luaLogTextBox_MouseLeave(object sender, MouseEventArgs e)
10311082
{
10321083
luaLogTextBox.IsEnabled = true;
@@ -1035,6 +1086,8 @@ private void luaLogTextBox_MouseLeave(object sender, MouseEventArgs e)
10351086
private void StopLuaButton_Click(object sender, RoutedEventArgs e)
10361087
{
10371088
luaLogCount = 0;
1089+
lock(luaLogsBuff)
1090+
luaLogsBuff.Clear();
10381091
if (!LuaEnv.LuaRunEnv.isRunning)
10391092
{
10401093
luaLogTextBox.Clear();

0 commit comments

Comments
 (0)