improve:优化timer

This commit is contained in:
2026-02-13 09:29:24 +08:00
parent a9fefadec9
commit 1d71f809cc
5 changed files with 93 additions and 47 deletions
+54 -14
View File
@@ -1,5 +1,6 @@
using Ink_Canvas.Helpers;
using Ink_Canvas.Helpers;
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
@@ -7,6 +8,7 @@ using System.Windows.Controls.Primitives;
using System.Windows.Forms;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Interop;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Threading;
@@ -19,17 +21,29 @@ namespace Ink_Canvas
{
public partial class MainWindow : Window
{
private const int WM_CLIPBOARDUPDATE = 0x031D;
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool AddClipboardFormatListener(IntPtr hwnd);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool RemoveClipboardFormatListener(IntPtr hwnd);
private bool isClipboardMonitoringEnabled;
private BitmapSource lastClipboardImage;
private HwndSource _clipboardHwndSource;
// 初始化剪贴板监控
private void InitializeClipboardMonitoring()
{
try
{
// 监听剪贴板变化
ClipboardNotification.ClipboardUpdate += OnClipboardUpdate;
isClipboardMonitoringEnabled = true;
SourceInitialized += OnSourceInitializedForClipboard;
}
catch (Exception ex)
{
@@ -37,6 +51,36 @@ namespace Ink_Canvas
}
}
private void OnSourceInitializedForClipboard(object sender, EventArgs e)
{
SourceInitialized -= OnSourceInitializedForClipboard;
try
{
var handle = new WindowInteropHelper(this).Handle;
if (handle == IntPtr.Zero) return;
_clipboardHwndSource = HwndSource.FromHwnd(handle);
_clipboardHwndSource?.AddHook(ClipboardWndProc);
if (!AddClipboardFormatListener(handle))
LogHelper.WriteLogToFile($"AddClipboardFormatListener 失败: {Marshal.GetLastWin32Error()}", LogHelper.LogType.Warning);
}
catch (Exception ex)
{
LogHelper.WriteLogToFile($"安装剪贴板监听失败: {ex.Message}", LogHelper.LogType.Error);
}
}
private IntPtr ClipboardWndProc(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
if (msg == WM_CLIPBOARDUPDATE)
{
Dispatcher.BeginInvoke(new Action(() => ClipboardNotification.NotifyFromMessage()), DispatcherPriority.Background);
handled = true;
}
return IntPtr.Zero;
}
// 剪贴板内容变化事件处理
private void OnClipboardUpdate()
{
@@ -273,6 +317,13 @@ namespace Ink_Canvas
ClipboardNotification.ClipboardUpdate -= OnClipboardUpdate;
isClipboardMonitoringEnabled = false;
}
var handle = new WindowInteropHelper(this).Handle;
if (handle != IntPtr.Zero)
RemoveClipboardFormatListener(handle);
_clipboardHwndSource?.RemoveHook(ClipboardWndProc);
_clipboardHwndSource = null;
}
catch (Exception ex)
{
@@ -286,19 +337,10 @@ namespace Ink_Canvas
{
public static event Action ClipboardUpdate;
private static Timer clipboardTimer;
private static string lastClipboardText = "";
private static bool lastHadImage;
static ClipboardNotification()
{
clipboardTimer = new Timer();
clipboardTimer.Interval = 500; // 每500ms检查一次
clipboardTimer.Tick += CheckClipboard;
clipboardTimer.Start();
}
private static void CheckClipboard(object sender, EventArgs e)
public static void NotifyFromMessage()
{
try
{
@@ -320,8 +362,6 @@ namespace Ink_Canvas
public static void Stop()
{
clipboardTimer?.Stop();
clipboardTimer?.Dispose();
}
}
}
+2 -2
View File
@@ -2357,9 +2357,9 @@ namespace Ink_Canvas
private void StartOrStoptimerCheckAutoFold()
{
if (Settings.Automation.IsEnableAutoFold)
timerCheckAutoFold.Start();
_unifiedMainWindowTimer?.Start();
else
timerCheckAutoFold.Stop();
_unifiedMainWindowTimer?.Stop();
}
private void ToggleSwitchAutoFoldInEasiNote_Toggled(object sender, RoutedEventArgs e)
+9 -7
View File
@@ -57,9 +57,8 @@ namespace Ink_Canvas
public partial class MainWindow : Window
{
private Timer timerCheckPPT = new Timer();
private Timer timerKillProcess = new Timer();
private Timer timerCheckAutoFold = new Timer();
private Timer _unifiedMainWindowTimer;
private string AvailableLatestVersion;
private Timer timerCheckAutoUpdateWithSilence = new Timer();
private Timer timerCheckAutoUpdateRetry = new Timer();
@@ -111,13 +110,11 @@ namespace Ink_Canvas
// 修改InitTimers方法中的初始时间和日期格式
private void InitTimers()
{
// PPT检查现在由PPTManager处理,不再需要定时器
// timerCheckPPT.Elapsed += TimerCheckPPT_Elapsed;
// timerCheckPPT.Interval = 500;
timerKillProcess.Elapsed += TimerKillProcess_Elapsed;
timerKillProcess.Interval = 2000;
timerCheckAutoFold.Elapsed += timerCheckAutoFold_Elapsed;
timerCheckAutoFold.Interval = 500;
_unifiedMainWindowTimer = new Timer(500);
_unifiedMainWindowTimer.Elapsed += OnUnifiedMainWindowTimerElapsed;
_unifiedMainWindowTimer.AutoReset = true;
timerCheckAutoUpdateWithSilence.Elapsed += timerCheckAutoUpdateWithSilence_Elapsed;
timerCheckAutoUpdateWithSilence.Interval = 1000 * 60 * 10;
timerCheckAutoUpdateRetry.Elapsed += timerCheckAutoUpdateRetry_Elapsed;
@@ -154,6 +151,11 @@ namespace Ink_Canvas
InitAutoSaveStrokesTimer();
}
private void OnUnifiedMainWindowTimerElapsed(object sender, ElapsedEventArgs e)
{
timerCheckAutoFold_Elapsed(sender, e);
}
// 初始化定时保存墨迹定时器
private void InitAutoSaveStrokesTimer()
{