diff --git a/MisakaTranslator-WPF/TranslateWindow.xaml.cs b/MisakaTranslator-WPF/TranslateWindow.xaml.cs index 964050e9..6983b808 100644 --- a/MisakaTranslator-WPF/TranslateWindow.xaml.cs +++ b/MisakaTranslator-WPF/TranslateWindow.xaml.cs @@ -53,6 +53,9 @@ public partial class TranslateWindow public volatile bool IsOCRingFlag; //线程锁:判断是否正在OCR线程中,保证同时只有一组在跑OCR public bool IsNotPausedFlag; //是否处在暂停状态(专用于OCR),为真可以翻译 + private static Timer ocrTimer; // ocr timing-task + private volatile bool ocrTimerPause; // ocr timing-task pause flag + private bool _isShowSource; //是否显示原文 private bool _isLocked; @@ -270,6 +273,22 @@ public static ITranslator TranslatorAuto(string translator) } } + /// + /// 定时事件 + /// + private void Hook_OnTimingActivity() + { + ocrTimerPause = false; + ocrTimer = new Timer(registerTimingOCR, null, 0, Common.UsingOCRDelay); + } + private void registerTimingOCR(object obj) + { + if (!ocrTimerPause) + { + TranslateEventOcr(isTimer: true); + } + } + /// /// 键盘点击事件 /// @@ -296,7 +315,7 @@ private void Hook_OnMouseActivity(object sender, POINT e) /// OCR事件 /// /// 是否是重新获取翻译 - private async void TranslateEventOcr(bool isRenew = false) + private async void TranslateEventOcr(bool isRenew = false, bool isTimer = false) { if (!IsNotPausedFlag && IsOCRingFlag) return; @@ -307,7 +326,7 @@ private async void TranslateEventOcr(bool isRenew = false) for (int i = 0; i < 3; i++) { // 重新OCR不需要等待 - if (!isRenew) + if (!isRenew || isTimer) await Task.Delay(Common.UsingOCRDelay); srcText = await Common.ocr.OCRProcessAsync(); @@ -714,6 +733,7 @@ private void Pause_Item_Click(object sender, RoutedEventArgs e) { PauseButton.SetValue(FontAwesome.WPF.Awesome.ContentProperty, FontAwesomeIcon.Play); } + ocrTimerPause = !ocrTimerPause; Common.textHooker.Pause = !Common.textHooker.Pause; } else @@ -751,6 +771,11 @@ private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs Common.appSettings.TF_SizeW = Convert.ToString((int)this.ActualWidth); Common.appSettings.TF_SizeH = Convert.ToString((int)this.ActualHeight); + if (ocrTimer != null) + { + ocrTimerPause = true; + ocrTimer.Dispose(); + } if (hook != null) { hook.Stop();