improve:PPT联动

This commit is contained in:
2026-02-13 00:35:32 +08:00
parent ed239929f3
commit 1c2e513c34
+42 -63
View File
@@ -1,4 +1,4 @@
using Microsoft.Office.Interop.PowerPoint; using Microsoft.Office.Interop.PowerPoint;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
@@ -128,9 +128,10 @@ namespace Ink_Canvas.Helpers
#endregion #endregion
#region Private Fields #region Private Fields
private Timer _connectionCheckTimer; private Timer _unifiedPptTimer;
private Timer _slideShowStateCheckTimer; private int _monitorTickCount;
private Timer _wpsProcessCheckTimer; private bool _isWpsMonitoringEnabled;
private Process _wpsProcess; private Process _wpsProcess;
private bool _isModuleUnloading = false; private bool _isModuleUnloading = false;
private bool _hasWpsProcessId; private bool _hasWpsProcessId;
@@ -151,62 +152,64 @@ namespace Ink_Canvas.Helpers
private void InitializeConnectionTimer() private void InitializeConnectionTimer()
{ {
_connectionCheckTimer = new Timer(500); _unifiedPptTimer = new Timer(500);
_connectionCheckTimer.Elapsed += OnConnectionCheckTimerElapsed; _unifiedPptTimer.Elapsed += OnUnifiedPptTimerElapsed;
_connectionCheckTimer.AutoReset = true; _unifiedPptTimer.AutoReset = true;
_slideShowStateCheckTimer = new Timer(1000);
_slideShowStateCheckTimer.Elapsed += OnSlideShowStateCheckTimerElapsed;
_slideShowStateCheckTimer.AutoReset = true;
} }
public void StartMonitoring() public void StartMonitoring()
{ {
if (!_disposed) if (!_disposed)
{ {
_connectionCheckTimer?.Start(); _unifiedPptTimer?.Start();
_slideShowStateCheckTimer?.Start();
LogHelper.WriteLogToFile("PPT监控已启动", LogHelper.LogType.Trace); LogHelper.WriteLogToFile("PPT监控已启动", LogHelper.LogType.Trace);
} }
} }
public void StopMonitoring() public void StopMonitoring()
{ {
_connectionCheckTimer?.Stop(); _unifiedPptTimer?.Stop();
_slideShowStateCheckTimer?.Stop(); StopWpsProcessCheckTimer();
DisconnectFromPPT(); DisconnectFromPPT();
LogHelper.WriteLogToFile("PPT监控已停止", LogHelper.LogType.Trace); LogHelper.WriteLogToFile("PPT监控已停止", LogHelper.LogType.Trace);
} }
#endregion #endregion
#region Connection Management #region Connection Management
private void OnConnectionCheckTimerElapsed(object sender, ElapsedEventArgs e) private void OnUnifiedPptTimerElapsed(object sender, ElapsedEventArgs e)
{ {
try if (_disposed || _isModuleUnloading)
{ {
if (!_isModuleUnloading) return;
{
CheckAndConnectToPPT();
}
} }
catch (Exception ex)
{
LogHelper.WriteLogToFile($"PPT连接检查失败: {ex}", LogHelper.LogType.Error);
}
}
private void OnSlideShowStateCheckTimerElapsed(object sender, ElapsedEventArgs e)
{
try 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) 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(); UpdateCurrentPresentationInfo();
// 停止连接检查定时器
_connectionCheckTimer?.Stop();
// 触发连接成功事件 // 触发连接成功事件
PPTConnectionChanged?.Invoke(true); PPTConnectionChanged?.Invoke(true);
@@ -466,8 +466,7 @@ namespace Ink_Canvas.Helpers
GC.Collect(); GC.Collect();
_isModuleUnloading = true; _isModuleUnloading = true;
_connectionCheckTimer?.Stop(); _unifiedPptTimer?.Stop();
_slideShowStateCheckTimer?.Stop();
// 触发连接断开事件 // 触发连接断开事件
PPTConnectionChanged?.Invoke(false); PPTConnectionChanged?.Invoke(false);
@@ -487,8 +486,7 @@ namespace Ink_Canvas.Helpers
Thread.Sleep(1000); Thread.Sleep(1000);
_isModuleUnloading = false; _isModuleUnloading = false;
_connectionCheckTimer?.Start(); _unifiedPptTimer?.Start();
_slideShowStateCheckTimer?.Start();
LogHelper.WriteLogToFile("PPT联动模块已重新加载", LogHelper.LogType.Trace); LogHelper.WriteLogToFile("PPT联动模块已重新加载", LogHelper.LogType.Trace);
} }
@@ -1398,20 +1396,11 @@ namespace Ink_Canvas.Helpers
{ {
if (!IsSupportWPS) return; if (!IsSupportWPS) return;
if (_wpsProcessCheckTimer != null) _isWpsMonitoringEnabled = true;
{ _wpsProcessCheckCount = 0;
_wpsProcessCheckTimer.Stop();
_wpsProcessCheckTimer.Dispose();
}
// 增加检查间隔到2秒,减少性能开销
_wpsProcessCheckTimer = new Timer(2000);
_wpsProcessCheckTimer.Elapsed += OnWpsProcessCheckTimerElapsed;
_wpsProcessCheckTimer.Start();
LogHelper.WriteLogToFile("启动 WPS 进程检测定时器", LogHelper.LogType.Trace);
} }
private void OnWpsProcessCheckTimerElapsed(object sender, ElapsedEventArgs e) private void CheckWpsProcess()
{ {
if (!IsSupportWPS) if (!IsSupportWPS)
{ {
@@ -1730,9 +1719,6 @@ namespace Ink_Canvas.Helpers
SlidesCount = 0; SlidesCount = 0;
StopWpsProcessCheckTimer(); StopWpsProcessCheckTimer();
// 重新启动连接检查定时器,以便能够检测新的WPS实例
_connectionCheckTimer?.Start();
// 触发连接断开事件 // 触发连接断开事件
PPTConnectionChanged?.Invoke(false); PPTConnectionChanged?.Invoke(false);
@@ -1744,12 +1730,7 @@ namespace Ink_Canvas.Helpers
private void StopWpsProcessCheckTimer() private void StopWpsProcessCheckTimer()
{ {
if (_wpsProcessCheckTimer != null) _isWpsMonitoringEnabled = false;
{
_wpsProcessCheckTimer.Stop();
_wpsProcessCheckTimer.Dispose();
_wpsProcessCheckTimer = null;
}
_wpsProcess = null; _wpsProcess = null;
_hasWpsProcessId = false; _hasWpsProcessId = false;
@@ -1757,7 +1738,7 @@ namespace Ink_Canvas.Helpers
_wpsProcessCheckCount = 0; _wpsProcessCheckCount = 0;
_lastForegroundWpsWindow = null; _lastForegroundWpsWindow = null;
_lastWindowCheckTime = DateTime.MinValue; _lastWindowCheckTime = DateTime.MinValue;
LogHelper.WriteLogToFile("停止 WPS 进程检测定时器", LogHelper.LogType.Trace); LogHelper.WriteLogToFile("停止 WPS 进程检测", LogHelper.LogType.Trace);
} }
#endregion #endregion
@@ -2081,9 +2062,7 @@ namespace Ink_Canvas.Helpers
StopMonitoring(); StopMonitoring();
StopWpsProcessCheckTimer(); StopWpsProcessCheckTimer();
_connectionCheckTimer?.Dispose(); _unifiedPptTimer?.Dispose();
_slideShowStateCheckTimer?.Dispose();
_wpsProcessCheckTimer?.Dispose();
_disposed = true; _disposed = true;
} }