using BaseFunction; using System; using System.Collections.Generic; using System.Text; namespace NSAnalysis { /// /// 刷新监控工具类,用于监控和诊断窗体刷新问题 /// public static class RefreshMonitor { private static Dictionary windowStats = new Dictionary(); private class WindowStats { public DateTime LastRefreshTime { get; set; } = DateTime.Now; public string LastVIN { get; set; } = ""; public int RefreshCount { get; set; } = 0; public int ErrorCount { get; set; } = 0; public int DataRetrievalCount { get; set; } = 0; public int EmptyDataCount { get; set; } = 0; public DateTime StartTime { get; set; } = DateTime.Now; } /// /// 记录刷新活动 /// /// 窗体名称 /// 当前VIN码 /// 是否成功 public static void LogRefreshActivity(string windowName, string vin, bool isSuccess = true) { if (!windowStats.ContainsKey(windowName)) { windowStats[windowName] = new WindowStats(); } var stats = windowStats[windowName]; stats.RefreshCount++; stats.LastRefreshTime = DateTime.Now; if (!isSuccess) { stats.ErrorCount++; } if (stats.LastVIN != vin) { MyBase.TraceWriteLine($"[RefreshMonitor] {windowName} - VIN变更: {stats.LastVIN} -> {vin}, 刷新次数: {stats.RefreshCount}, 错误次数: {stats.ErrorCount}"); stats.LastVIN = vin; } // 每100次刷新记录一次统计信息 if (stats.RefreshCount % 100 == 0) { var successRate = ((stats.RefreshCount - stats.ErrorCount) * 100.0 / stats.RefreshCount); var runTime = DateTime.Now - stats.StartTime; MyBase.TraceWriteLine($"[RefreshMonitor] {windowName} - 统计: 总刷新{stats.RefreshCount}次, 错误{stats.ErrorCount}次, 成功率: {successRate:F1}%, 运行时间: {runTime.TotalHours:F1}小时"); } } /// /// 记录数据库查询结果 /// /// 窗体名称 /// 查询类型 /// 查询结果 /// 数据行数 public static void LogDatabaseQuery(string windowName, string queryType, string result, int rowCount = 0) { if (!windowStats.ContainsKey(windowName)) { windowStats[windowName] = new WindowStats(); } var stats = windowStats[windowName]; stats.DataRetrievalCount++; if (rowCount == 0) { stats.EmptyDataCount++; } MyBase.TraceWriteLine($"[RefreshMonitor] {windowName} - 数据库查询: {queryType}, 结果: {result}, 行数: {rowCount}"); } /// /// 记录UI更新情况 /// /// 窗体名称 /// 更新类型 /// 更新的控件数量 public static void LogUIUpdate(string windowName, string updateType, int controlCount) { MyBase.TraceWriteLine($"[RefreshMonitor] {windowName} - UI更新: {updateType}, 控件数量: {controlCount}"); } /// /// 检查是否长时间未刷新 /// /// 窗体名称 /// 最大允许的未刷新分钟数 /// 是否超时 public static bool CheckRefreshTimeout(string windowName, int maxMinutes = 5) { if (!windowStats.ContainsKey(windowName)) { return false; } var stats = windowStats[windowName]; var timeSinceLastRefresh = DateTime.Now - stats.LastRefreshTime; if (timeSinceLastRefresh.TotalMinutes > maxMinutes) { MyBase.TraceWriteLine($"[RefreshMonitor] 警告: {windowName} 已经 {timeSinceLastRefresh.TotalMinutes:F1} 分钟未刷新!"); return true; } return false; } /// /// 重置指定窗体的监控统计 /// /// 窗体名称,为空则重置所有 public static void Reset(string windowName = null) { if (string.IsNullOrEmpty(windowName)) { windowStats.Clear(); MyBase.TraceWriteLine("[RefreshMonitor] 所有窗体监控统计已重置"); } else if (windowStats.ContainsKey(windowName)) { windowStats[windowName] = new WindowStats(); MyBase.TraceWriteLine($"[RefreshMonitor] {windowName} 监控统计已重置"); } } /// /// 获取指定窗体的统计信息 /// /// 窗体名称 /// 统计信息字符串 public static string GetStatistics(string windowName) { if (!windowStats.ContainsKey(windowName)) { return $"{windowName}: 无统计数据"; } var stats = windowStats[windowName]; var timeSinceLastRefresh = DateTime.Now - stats.LastRefreshTime; var successRate = ((stats.RefreshCount - stats.ErrorCount) * 100.0 / Math.Max(stats.RefreshCount, 1)); var runTime = DateTime.Now - stats.StartTime; return $"{windowName}: 总刷新{stats.RefreshCount}, 错误{stats.ErrorCount}, 成功率{successRate:F1}%, " + $"数据查询{stats.DataRetrievalCount}, 空数据{stats.EmptyDataCount}, " + $"上次刷新{timeSinceLastRefresh.TotalMinutes:F1}分钟前, 运行{runTime.TotalHours:F1}小时"; } /// /// 获取所有窗体的统计信息 /// /// 所有统计信息 public static string GetAllStatistics() { if (windowStats.Count == 0) { return "无监控数据"; } var sb = new StringBuilder(); sb.AppendLine("=== 窗体刷新监控统计 ==="); foreach (var kvp in windowStats) { sb.AppendLine(GetStatistics(kvp.Key)); } return sb.ToString(); } } }