From 1c2e513c349f43c0729e8d9c6be2e9e7d943be8d Mon Sep 17 00:00:00 2001 From: CJKmkp <2564608840@qq.com> Date: Fri, 13 Feb 2026 00:35:32 +0800 Subject: [PATCH] =?UTF-8?q?improve:PPT=E8=81=94=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Ink Canvas/Helpers/PPTManager.cs | 105 +++++++++++++------------------ 1 file changed, 42 insertions(+), 63 deletions(-) diff --git a/Ink Canvas/Helpers/PPTManager.cs b/Ink Canvas/Helpers/PPTManager.cs index ba6b6898..227f012a 100644 --- a/Ink Canvas/Helpers/PPTManager.cs +++ b/Ink Canvas/Helpers/PPTManager.cs @@ -1,4 +1,4 @@ -using Microsoft.Office.Interop.PowerPoint; +using Microsoft.Office.Interop.PowerPoint; using System; using System.Collections.Generic; using System.Diagnostics; @@ -128,9 +128,10 @@ namespace Ink_Canvas.Helpers #endregion #region Private Fields - private Timer _connectionCheckTimer; - private Timer _slideShowStateCheckTimer; - private Timer _wpsProcessCheckTimer; + private Timer _unifiedPptTimer; + private int _monitorTickCount; + private bool _isWpsMonitoringEnabled; + private Process _wpsProcess; private bool _isModuleUnloading = false; private bool _hasWpsProcessId; @@ -151,62 +152,64 @@ namespace Ink_Canvas.Helpers private void InitializeConnectionTimer() { - _connectionCheckTimer = new Timer(500); - _connectionCheckTimer.Elapsed += OnConnectionCheckTimerElapsed; - _connectionCheckTimer.AutoReset = true; - - _slideShowStateCheckTimer = new Timer(1000); - _slideShowStateCheckTimer.Elapsed += OnSlideShowStateCheckTimerElapsed; - _slideShowStateCheckTimer.AutoReset = true; + _unifiedPptTimer = new Timer(500); + _unifiedPptTimer.Elapsed += OnUnifiedPptTimerElapsed; + _unifiedPptTimer.AutoReset = true; } public void StartMonitoring() { if (!_disposed) { - _connectionCheckTimer?.Start(); - _slideShowStateCheckTimer?.Start(); + _unifiedPptTimer?.Start(); LogHelper.WriteLogToFile("PPT监控已启动", LogHelper.LogType.Trace); } } public void StopMonitoring() { - _connectionCheckTimer?.Stop(); - _slideShowStateCheckTimer?.Stop(); + _unifiedPptTimer?.Stop(); + StopWpsProcessCheckTimer(); DisconnectFromPPT(); LogHelper.WriteLogToFile("PPT监控已停止", LogHelper.LogType.Trace); } #endregion #region Connection Management - private void OnConnectionCheckTimerElapsed(object sender, ElapsedEventArgs e) + private void OnUnifiedPptTimerElapsed(object sender, ElapsedEventArgs e) { - try + if (_disposed || _isModuleUnloading) { - if (!_isModuleUnloading) - { - CheckAndConnectToPPT(); - } + return; } - catch (Exception ex) - { - LogHelper.WriteLogToFile($"PPT连接检查失败: {ex}", LogHelper.LogType.Error); - } - } - private void OnSlideShowStateCheckTimerElapsed(object sender, ElapsedEventArgs e) - { try { - if (!_isModuleUnloading && IsConnected) + var tick = Interlocked.Increment(ref _monitorTickCount); + + CheckAndConnectToPPT(); + + if (IsConnected) { - CheckSlideShowState(); + if (tick % 2 == 0) + { + CheckSlideShowState(); + } + + if (_isWpsMonitoringEnabled && tick % 4 == 0) + { + CheckWpsProcess(); + } + } + + if (tick >= int.MaxValue - 1000) + { + Interlocked.Exchange(ref _monitorTickCount, 0); } } catch (Exception ex) { - LogHelper.WriteLogToFile($"PPT放映状态检查失败: {ex}", LogHelper.LogType.Error); + LogHelper.WriteLogToFile($"统一PPT监控定时器执行失败: {ex}", LogHelper.LogType.Error); } } @@ -361,9 +364,6 @@ namespace Ink_Canvas.Helpers // 获取当前演示文稿信息 UpdateCurrentPresentationInfo(); - // 停止连接检查定时器 - _connectionCheckTimer?.Stop(); - // 触发连接成功事件 PPTConnectionChanged?.Invoke(true); @@ -466,8 +466,7 @@ namespace Ink_Canvas.Helpers GC.Collect(); _isModuleUnloading = true; - _connectionCheckTimer?.Stop(); - _slideShowStateCheckTimer?.Stop(); + _unifiedPptTimer?.Stop(); // 触发连接断开事件 PPTConnectionChanged?.Invoke(false); @@ -487,8 +486,7 @@ namespace Ink_Canvas.Helpers Thread.Sleep(1000); _isModuleUnloading = false; - _connectionCheckTimer?.Start(); - _slideShowStateCheckTimer?.Start(); + _unifiedPptTimer?.Start(); LogHelper.WriteLogToFile("PPT联动模块已重新加载", LogHelper.LogType.Trace); } @@ -1398,20 +1396,11 @@ namespace Ink_Canvas.Helpers { if (!IsSupportWPS) return; - if (_wpsProcessCheckTimer != null) - { - _wpsProcessCheckTimer.Stop(); - _wpsProcessCheckTimer.Dispose(); - } - - // 增加检查间隔到2秒,减少性能开销 - _wpsProcessCheckTimer = new Timer(2000); - _wpsProcessCheckTimer.Elapsed += OnWpsProcessCheckTimerElapsed; - _wpsProcessCheckTimer.Start(); - LogHelper.WriteLogToFile("启动 WPS 进程检测定时器", LogHelper.LogType.Trace); + _isWpsMonitoringEnabled = true; + _wpsProcessCheckCount = 0; } - private void OnWpsProcessCheckTimerElapsed(object sender, ElapsedEventArgs e) + private void CheckWpsProcess() { if (!IsSupportWPS) { @@ -1730,9 +1719,6 @@ namespace Ink_Canvas.Helpers SlidesCount = 0; StopWpsProcessCheckTimer(); - // 重新启动连接检查定时器,以便能够检测新的WPS实例 - _connectionCheckTimer?.Start(); - // 触发连接断开事件 PPTConnectionChanged?.Invoke(false); @@ -1744,12 +1730,7 @@ namespace Ink_Canvas.Helpers private void StopWpsProcessCheckTimer() { - if (_wpsProcessCheckTimer != null) - { - _wpsProcessCheckTimer.Stop(); - _wpsProcessCheckTimer.Dispose(); - _wpsProcessCheckTimer = null; - } + _isWpsMonitoringEnabled = false; _wpsProcess = null; _hasWpsProcessId = false; @@ -1757,7 +1738,7 @@ namespace Ink_Canvas.Helpers _wpsProcessCheckCount = 0; _lastForegroundWpsWindow = null; _lastWindowCheckTime = DateTime.MinValue; - LogHelper.WriteLogToFile("停止 WPS 进程检测定时器", LogHelper.LogType.Trace); + LogHelper.WriteLogToFile("停止 WPS 进程检测", LogHelper.LogType.Trace); } #endregion @@ -2081,9 +2062,7 @@ namespace Ink_Canvas.Helpers StopMonitoring(); StopWpsProcessCheckTimer(); - _connectionCheckTimer?.Dispose(); - _slideShowStateCheckTimer?.Dispose(); - _wpsProcessCheckTimer?.Dispose(); + _unifiedPptTimer?.Dispose(); _disposed = true; }