优化日志事件接收方法, 修复启动控制器时没有清除上次启动的日志

This commit is contained in:
ChsBuffer
2020-07-30 21:31:25 +08:00
parent 4e5bdab6b8
commit 5495b94513
2 changed files with 15 additions and 27 deletions

View File

@@ -3,7 +3,9 @@ using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Netch.Models;
using Netch.Utils;
@@ -137,40 +139,24 @@ namespace Netch.Controllers
/// <param name="e">数据</param>
protected void OnOutputDataReceived(object sender, DataReceivedEventArgs e)
{
var str = Encoding.UTF8.GetString(Encoding.GetEncoding("gbk").GetBytes(e.Data??string.Empty));
// 程序结束接收到 null
if (e.Data == null)
{
State = State.Stopped;
return;
}
var str = Encoding.UTF8.GetString(Encoding.GetEncoding("gbk").GetBytes(e.Data ?? string.Empty));
// 写入日志
if (!Write(str)) return;
Task.Run(() => Write(str));
// 检查启动
if (State == State.Starting)
{
if (Instance.HasExited)
{
if (_startedKeywords.Any(s => str.Contains(s)))
State = State.Started;
else if (_stoppedKeywords.Any(s => str.Contains(s)))
State = State.Stopped;
return;
}
foreach (var s in _startedKeywords)
{
if (str.Contains(s))
{
State = State.Started;
return;
}
}
foreach (var s in _stoppedKeywords)
{
if (str.Contains(s))
{
State = State.Stopped;
return;
}
}
}
}
}

View File

@@ -86,6 +86,7 @@ namespace Netch.Controllers
}
Global.MainForm.StatusText(i18N.Translate("Starting ", pEncryptedProxyController.Name));
pEncryptedProxyController.ClearLog();
result = pEncryptedProxyController.Start(server, mode);
}
@@ -112,6 +113,7 @@ namespace Netch.Controllers
if (pModeController != null)
{
Global.MainForm.StatusText(i18N.Translate("Starting ", pModeController.Name));
pModeController.ClearLog();
result = pModeController.Start(server, mode);
}