protected override async Task ExecuteAsync(CancellationToken stoppingToken) {…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
protected override async Task ExecuteAsync(CancellationToken stoppingToken) { _logger.LogInformation("后台控制台服务启动"); Console.Title = $"白屏自动化,服务器: {authority},工作号:{account}"; while (!stoppingToken.IsCancellationRequested) { await Task.Delay(1000 * 2, stoppingToken); clyg_sgui_cluster_node node = dal.SelectclusterByauthority(authority); if (node == null) { node = new clyg_sgui_cluster_node() { account= account, authority= authority, expired=false, locker=string.Empty, timeout=0, online=true, version= Guid.NewGuid().ToString(), lastcall = DateTimeOffset.Now.ToUnixTimeMilliseconds() }; //添加进去 这种情况是第一次部署新的服务器会出现 dal.Insertcluster(node); } try { Window window = null; Process Process = null; Application app = null; //查状态
var SguiProcess = Process.GetProcessesByName("共翔天易");
if (SguiProcess.Length == 0) { Console.WriteLine($"程序未启动或异常关闭,请及时查看!"); isUpdateStart(node.online ? 1 : 0, 0); continue; }
Process = SguiProcess.First();
app = FlaUI.Core.Application.Attach(Process.Id);
using (var automation = new UIA3Automation())
{
Thread.Sleep(500);
window = app.GetMainWindow(automation);
Thread.Sleep(500);
var loginBtn = window.FindFirstDescendant(cf => cf.ByName("登录"));
if (loginBtn != null) { SMSHelper.DingSendMessage(dingerrorkey, "通知", $"白屏账号{account}已经失效,请及时查看!!:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}!!!", new List<string>() { }, new List<string>(), false, dingerrortoken); isUpdateStart(node.online ? 1 : 0, 0); continue; }
//查看定编按钮是否可用
var createPnrBtn = window.FindFirstDescendant(cf => cf.ByName("新建PNR"));
if (createPnrBtn == null) { SMSHelper.DingSendMessage(dingerrorkey, "通知", $"白屏账号{account}没有检测到新建pnr的按钮,请及时查看问题!!:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}!!!", new List<string>() { }, new List<string>(), false, dingerrortoken); Console.WriteLine("没有检测到新建pnr的按钮,请及时查看问题!"); isUpdateStart(node.online ? 1 : 0, 0); continue; }
//检测是否有弹窗(登录超时)未登录或登录超时
var IsLogin = window.FindFirstDescendant(cf => cf.ByName("未登录或登录超时"));
if (IsLogin != null) { SMSHelper.DingSendMessage(dingerrorkey, "通知", $"白屏账号{account}未登录或登录超时,请及时查看!!:{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}!!!", new List<string>() { }, new List<string>(), false, dingerrortoken); Console.WriteLine("未登录或登录超时,请及时查看!"); isUpdateStart(node.online ? 1 : 0, 0); continue; }
} ;
//检查通道号是否更换 这种情况是服务器上更换通道号 或者 服务器白屏登录更换账号 会导致这种问题
if (node.account != account)
{
dal.Updateclusteraccount(authority,account);
}
//正常状态
isUpdateStart(node.online ? 1 : 0, 1);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
} 我该如何优化我的程序,减少内容的异常占用,我是用的是FlaUI.UIA3库3.2版本
你这段代码已经能跑起来,但确实存在资源占用、异常处理、性能等方面的问题。下面我会从 FlaUI 3.2 的正确使用方式 出发,给你一个优化后的版本,并解释每一步为什么这么做。
AutomationElement 不释放,但 UIA3Automation 和 Application 必须释放)Thread.Sleep)protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
_logger.LogInformation("后台控制台服务启动");
Console.Title = $"白屏自动化,服务器: {authority},工作号:{account}";
while (!stoppingToken.IsCancellationRequested)
{
await Task.Delay(2000, stoppingToken);
var node = dal.SelectclusterByauthority(authority) ?? new clyg_sgui_cluster_node
{
account = account,
authority = authority,
expired = false,
locker = string.Empty,
timeout = 0,
online = true,
version = Guid.NewGuid().ToString(),
lastcall = DateTimeOffset.Now.ToUnixTimeMilliseconds()
};
if (dal.SelectclusterByauthority(authority) == null)
dal.Insertcluster(node);
try
{
var processes = Process.GetProcessesByName("共翔天易");
if (processes.Length == 0)
{
Console.WriteLine("程序未启动或异常关闭");
isUpdateStart(node.online ? 1 : 0, 0);
continue;
}
var process = processes.First();
using var automation = new UIA3Automation();
using var app = FlaUI.Core.Application.Attach(process.Id);
using var window = app.GetMainWindow(automation, TimeSpan.FromSeconds(5));
if (window == null)
{
Console.WriteLine("窗口未找到");
isUpdateStart(node.online ? 1 : 0, 0);
continue;
}
// 登录按钮检测
if (window.FindFirstDescendant(cf => cf.ByName("登录")) != null)
{
SMSHelper.DingSendMessage(...);
isUpdateStart(node.online ? 1 : 0, 0);
continue;
}
// 新建PNR按钮检测
if (window.FindFirstDescendant(cf => cf.ByName("新建PNR")) == null)
{
SMSHelper.DingSendMessage(...);
isUpdateStart(node.online ? 1 : 0, 0);
continue;
}
// 登录超时弹窗检测
if (window.FindFirstDescendant(cf => cf.ByName("未登录或登录超时")) != null)
{
SMSHelper.DingSendMessage(...);
isUpdateStart(node.online ? 1 : 0, 0);
continue;
}
// 检查账号是否变更
if (node.account != account)
{
dal.Updateclusteraccount(authority, account);
}
isUpdateStart(node.online ? 1 : 0, 1);
}
catch (FlaUI.Core.Exceptions.ElementNotFoundException ex)
{
Console.WriteLine($"元素未找到: {ex.Message}");
isUpdateStart(node.online ? 1 : 0, 0);
}
catch (FlaUI.Core.Exceptions.TimeoutException ex)
{
Console.WriteLine($"超时: {ex.Message}");
isUpdateStart(node.online ? 1 : 0, 0);
}
catch (Exception ex)
{
Console.WriteLine($"未知异常: {ex.Message}");
isUpdateStart(node.online ? 1 : 0, 0);
}
}
}
| 问题 | 优化方式 |
|---|---|
| 资源泄漏 | 使用 using 自动释放 UIA3Automation 和 Application |
| 重复 Attach | 每次循环重新 Attach,避免残留句柄 |
| Thread.Sleep | 改为 await Task.Delay,不阻塞线程 |
| 异常处理 | 捕获 FlaUI 的 ElementNotFoundException 和 TimeoutException |
| 窗口未就绪 | 使用 GetMainWindow(timeout) 避免死等 |
| 逻辑冗余 | 合并重复判断,减少嵌套 |
var cacheRequest = new CacheRequest();
cacheRequest.Add(AutomationElement.NameProperty);
using (cacheRequest.Activate())
{
var loginBtn = window.FindFirstDescendant(cf => cf.ByName("登录"));
}
RetryPolicy(Polly)如果你担心窗口偶尔未加载完,可以用 Polly 做重试:
Policy
.HandleResult<AutomationElement>(x => x == null)
.WaitAndRetry(3, _ => TimeSpan.FromSeconds(1))
.Execute(() => window.FindFirstDescendant(cf => cf.ByName("登录")));
在 FlaUI 3.2 中,不要释放
AutomationElement,但要确保UIA3Automation和Application每次循环都正确释放。
如果你愿意,我可以帮你封装一个 SafeFlaUIContext 类,自动管理这些资源。