Files
CapMachine/CapMachine.Wpf/Views/MainView.xaml.cs
2026-03-27 17:41:55 +08:00

169 lines
5.9 KiB
C#
Raw Permalink 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 CapMachine.Wpf.Services;
using System;
using System.Windows;
namespace CapMachine.Wpf.Views
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainView : Window
{
private readonly ZlgCanDriveService _zlgCanDriveService;
private readonly ZlgLinDriveService _zlgLinDriveService;
private readonly CanDriveService _canDriveService;
private readonly LinDriveService _linDriveService;
public MainView(ILogService logService, ZlgCanDriveService zlgCanDriveService, ZlgLinDriveService zlgLinDriveService, CanDriveService canDriveService, LinDriveService linDriveService)
{
InitializeComponent();
LogService = logService;
_zlgCanDriveService = zlgCanDriveService;
_zlgLinDriveService = zlgLinDriveService;
_canDriveService = canDriveService;
_linDriveService = linDriveService;
}
public ILogService LogService { get; }
/// <summary>
/// Windows状态
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_StateChanged(object sender, EventArgs e)
{
LogService.Info($"Windows状态{this.WindowState}-Visibility: {this.Visibility}");
}
/// <summary>
/// 主界面正要关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
// 弹窗提示是否确定要退出
MessageBoxResult result = MessageBox.Show("您确定要退出程序吗?", "提示", MessageBoxButton.OKCancel, MessageBoxImage.None, MessageBoxResult.Cancel);
//System.Console.WriteLine(result);
if (result == MessageBoxResult.Cancel)
{
e.Cancel = true; // 中断点击事件
}
else
{
StopBusCommunicationOnExit();
LogService.Info("Windows关闭");
}
}
/// <summary>
/// 软件退出时停止总线通信,防止适配器在进程退出后继续自动发送。
/// </summary>
private void StopBusCommunicationOnExit()
{
StopZlgBusCommunicationOnExit();
StopToomossBusCommunicationOnExit();
}
/// <summary>
/// 停止 ZLG CAN/CANFD 与 LIN 通信。
/// </summary>
private void StopZlgBusCommunicationOnExit()
{
try
{
if (_zlgLinDriveService.OpenState)
{
if (_zlgLinDriveService.IsCycleSend || _zlgLinDriveService.SchEnable)
{
_zlgLinDriveService.StopSchedule();
_zlgLinDriveService.IsCycleSend = false;
}
_zlgLinDriveService.CloseDevice();
LogService.Info("程序退出时已停止 ZLG LIN 调度/发送并关闭设备");
return;
}
if (_zlgCanDriveService.OpenState)
{
if (_zlgCanDriveService.IsCycleSend || _zlgCanDriveService.SchEnable)
{
_zlgCanDriveService.StopSchedule();
_zlgCanDriveService.IsCycleSend = false;
}
_zlgCanDriveService.CloseDevice();
LogService.Info("程序退出时已停止 ZLG CAN/CANFD 调度/发送并关闭设备");
}
}
catch (Exception ex)
{
LogService.Error($"程序退出停止 ZLG 总线失败:{ex.Message}");
}
}
/// <summary>
/// 停止 Toomoss CAN 与 LIN 通信。
/// </summary>
private void StopToomossBusCommunicationOnExit()
{
try
{
if (_linDriveService.ToomossLinDrive.OpenState)
{
if (_linDriveService.ToomossLinDrive.IsCycleSend)
{
_linDriveService.ToomossLinDrive.StopCycleSendMsg();
}
if (_linDriveService.ToomossLinDrive.SchEnable)
{
_linDriveService.ToomossLinDrive.StopSchedule();
}
_linDriveService.ToomossLinDrive.CloseDevice();
LogService.Info("程序退出时已停止 Toomoss LIN 调度/发送并关闭设备");
}
}
catch (Exception ex)
{
LogService.Error($"程序退出停止 Toomoss LIN 失败:{ex.Message}");
}
try
{
if (_canDriveService.ToomossCanDrive.OpenState)
{
if (_canDriveService.ToomossCanDrive.IsCycleSend)
{
_canDriveService.ToomossCanDrive.StopCycleSendMsg();
}
if (_canDriveService.ToomossCanDrive.SchEnable)
{
_canDriveService.ToomossCanDrive.StopSchedule();
}
_canDriveService.ToomossCanDrive.CloseDevice();
LogService.Info("程序退出时已停止 Toomoss CAN 调度/发送并关闭设备");
}
}
catch (Exception ex)
{
LogService.Error($"程序退出停止 Toomoss CAN 失败:{ex.Message}");
}
}
/// <summary>
/// 主界面完成关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Window_Closed(object sender, EventArgs e)
{
Environment.Exit(0); // 强制结束
}
}
}