Files

226 lines
7.6 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using BaseFunction;
namespace NSAnalysis
{
/// <summary>
/// 日志统计窗体,用于显示所有窗体的运行统计信息
/// </summary>
public partial class LogStatisticsForm : Form
{
private Timer refreshTimer;
private TextBox txtStatistics;
private Button btnRefresh;
private Button btnReset;
private Button btnExport;
public LogStatisticsForm()
{
InitializeComponent();
SetupControls();
SetupTimer();
}
private void InitializeComponent()
{
this.Text = "窗体刷新统计监控";
this.Size = new Size(800, 600);
this.StartPosition = FormStartPosition.CenterScreen;
this.FormBorderStyle = FormBorderStyle.Sizable;
this.MinimumSize = new Size(600, 400);
}
private void SetupControls()
{
// 创建控件
txtStatistics = new TextBox
{
Multiline = true,
ScrollBars = ScrollBars.Both,
ReadOnly = true,
Font = new Font("Consolas", 9),
Dock = DockStyle.Fill
};
Panel buttonPanel = new Panel
{
Height = 40,
Dock = DockStyle.Bottom
};
btnRefresh = new Button
{
Text = "刷新统计",
Size = new Size(80, 30),
Location = new Point(10, 5)
};
btnReset = new Button
{
Text = "重置统计",
Size = new Size(80, 30),
Location = new Point(100, 5)
};
btnExport = new Button
{
Text = "导出日志",
Size = new Size(80, 30),
Location = new Point(190, 5)
};
// 添加事件处理
btnRefresh.Click += BtnRefresh_Click;
btnReset.Click += BtnReset_Click;
btnExport.Click += BtnExport_Click;
// 添加控件到窗体
buttonPanel.Controls.Add(btnRefresh);
buttonPanel.Controls.Add(btnReset);
buttonPanel.Controls.Add(btnExport);
this.Controls.Add(txtStatistics);
this.Controls.Add(buttonPanel);
}
private void SetupTimer()
{
refreshTimer = new Timer
{
Interval = 5000 // 每5秒自动刷新
};
refreshTimer.Tick += RefreshTimer_Tick;
refreshTimer.Start();
}
private void RefreshTimer_Tick(object sender, EventArgs e)
{
RefreshStatistics();
}
private void BtnRefresh_Click(object sender, EventArgs e)
{
RefreshStatistics();
}
private void BtnReset_Click(object sender, EventArgs e)
{
var result = MessageBox.Show("确定要重置所有窗体的统计信息吗?", "确认重置",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (result == DialogResult.Yes)
{
RefreshMonitor.Reset();
RefreshStatistics();
MyBase.TraceWriteLine("[LogStatisticsForm] 用户手动重置了所有统计信息");
}
}
private void BtnExport_Click(object sender, EventArgs e)
{
try
{
SaveFileDialog saveDialog = new SaveFileDialog
{
Filter = "文本文件|*.txt|所有文件|*.*",
FileName = $"窗体刷新统计_{DateTime.Now:yyyyMMdd_HHmmss}.txt"
};
if (saveDialog.ShowDialog() == DialogResult.OK)
{
System.IO.File.WriteAllText(saveDialog.FileName, txtStatistics.Text, Encoding.UTF8);
MessageBox.Show($"统计信息已导出到:{saveDialog.FileName}", "导出成功",
MessageBoxButtons.OK, MessageBoxIcon.Information);
MyBase.TraceWriteLine($"[LogStatisticsForm] 统计信息已导出到: {saveDialog.FileName}");
}
}
catch (Exception ex)
{
MessageBox.Show($"导出失败:{ex.Message}", "错误",
MessageBoxButtons.OK, MessageBoxIcon.Error);
MyBase.TraceWriteLine($"[LogStatisticsForm] 导出失败: {ex.Message}");
}
}
private void RefreshStatistics()
{
try
{
var sb = new StringBuilder();
sb.AppendLine($"=== 窗体刷新监控统计 ===");
sb.AppendLine($"更新时间: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
sb.AppendLine();
// 获取所有窗体的统计信息
string[] windowNames = { "FEHYLeftCarData", "FEH3LeftCarData", "FEHYRightCarData", "FEH3RightCarData" };
foreach (string windowName in windowNames)
{
sb.AppendLine($"【{windowName}】");
sb.AppendLine($" {RefreshMonitor.GetStatistics(windowName)}");
// 检查是否有超时
if (RefreshMonitor.CheckRefreshTimeout(windowName, 5))
{
sb.AppendLine($" ⚠️ 警告: 该窗体可能已停止刷新!");
}
sb.AppendLine();
}
// 添加系统信息
sb.AppendLine("=== 系统信息 ===");
sb.AppendLine($"当前时间: {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
sb.AppendLine($"系统运行时间: {Environment.TickCount / 1000 / 60:F1} 分钟");
sb.AppendLine($"内存使用: {GC.GetTotalMemory(false) / 1024 / 1024:F1} MB");
txtStatistics.Text = sb.ToString();
// 自动滚动到底部
txtStatistics.SelectionStart = txtStatistics.Text.Length;
txtStatistics.ScrollToCaret();
}
catch (Exception ex)
{
txtStatistics.Text = $"刷新统计信息时出错: {ex.Message}";
MyBase.TraceWriteLine($"[LogStatisticsForm] 刷新统计信息异常: {ex.Message}");
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
refreshTimer?.Stop();
refreshTimer?.Dispose();
MyBase.TraceWriteLine("[LogStatisticsForm] 统计监控窗体已关闭");
base.OnFormClosing(e);
}
/// <summary>
/// 显示统计窗体(单例模式)
/// </summary>
private static LogStatisticsForm instance;
public static void ShowStatistics()
{
if (instance == null || instance.IsDisposed)
{
instance = new LogStatisticsForm();
}
if (!instance.Visible)
{
instance.Show();
}
else
{
instance.BringToFront();
}
}
}
}