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.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;
}