Files
Axi_Omron/src/AxiOmron.PcbCheck/Utils/ShutdownHelper.cs
yunxiao.zhu d70b94e904 feat(*): 添加扫码枪启动探活、全局退出助手及 README
- 添加扫码枪串口启动探活,检测端口占用并更新 UI 状态
- 新增 ShutdownHelper 安全停止 Host 扩展方法
- 新增 README.md 项目说明文档
- 更新 WorkflowHostedService 启动探活逻辑
- 补充 ShutdownHelper 与 WorkflowHostedService 单元测试
- 优化 DashboardPage 与 SystemSettingsPage 界面布局
- 调整 ModbusTcpPlcService 监控镜像读取逻辑
2026-04-19 14:29:07 +08:00

27 lines
1.1 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.Extensions.Hosting;
namespace AxiOmron.PcbCheck.Utils;
/// <summary>
/// 提供应用退出阶段的异步宿主停止辅助能力,确保 await 后续逻辑保留在捕获到的同步上下文中。
/// </summary>
internal static class ShutdownHelper
{
/// <summary>
/// 停止指定 Host并在停止完成后于捕获的调用上下文中执行收尾回调。
/// </summary>
/// <param name="host">待停止的宿主实例。</param>
/// <param name="timeout">停止超时时间。</param>
/// <param name="afterStop">停止完成后的收尾回调。</param>
/// <returns>表示停止流程完成的任务。</returns>
/// <exception cref="ArgumentNullException">当 <paramref name="host"/> 为空时抛出。</exception>
internal static async Task StopHostAsync(IHost host, TimeSpan timeout, Action? afterStop = null)
{
ArgumentNullException.ThrowIfNull(host);
using var cancellationTokenSource = new CancellationTokenSource(timeout);
await host.StopAsync(cancellationTokenSource.Token);
afterStop?.Invoke();
}
}