From 800f01cc654fa73eae8c26acb6053a838e414883 Mon Sep 17 00:00:00 2001 From: Nikolaj Olsson Date: Wed, 20 Nov 2024 19:15:37 +0100 Subject: [PATCH] Minor performance optimization --- src/ui/Controls/AudioVisualizer.cs | 9 +-- src/ui/Controls/SimpleTextBox.cs | 9 +-- src/ui/Forms/AudioToText/VoskAudioToText.cs | 4 +- .../VoskAudioToTextSelectedLines.cs | 5 +- .../Forms/AudioToText/WhisperAudioToText.cs | 4 +- .../GenerateTransparentVideoWithSubtitles.cs | 4 +- src/ui/Forms/GenerateVideo.cs | 4 +- src/ui/Forms/GenerateVideoWithHardSubs.cs | 8 +-- src/ui/Forms/Main.cs | 70 +++++++++---------- src/ui/Forms/Ocr/VobSubOcr.cs | 7 +- src/ui/Forms/Options/WordLists.cs | 7 +- src/ui/Logic/UiUtil.cs | 6 +- src/ui/Logic/WindowsHelper.cs | 6 +- 13 files changed, 74 insertions(+), 69 deletions(-) diff --git a/src/ui/Controls/AudioVisualizer.cs b/src/ui/Controls/AudioVisualizer.cs index 30fec3dee..efcf6a178 100644 --- a/src/ui/Controls/AudioVisualizer.cs +++ b/src/ui/Controls/AudioVisualizer.cs @@ -11,6 +11,7 @@ using System.Runtime.CompilerServices; using System.Threading.Tasks; using System.Windows.Forms; using Nikse.SubtitleEdit.Core.Forms; +using System.Diagnostics; namespace Nikse.SubtitleEdit.Controls { @@ -486,7 +487,7 @@ namespace Nikse.SubtitleEdit.Controls public void SetPosition(double startPositionSeconds, Subtitle subtitle, double currentVideoPositionSeconds, int subtitleIndex, ListView.SelectedIndexCollection selectedIndexes) { - if (TimeSpan.FromTicks(DateTime.UtcNow.Ticks - _lastMouseWheelScroll).TotalSeconds > 0.25) + if (TimeSpan.FromTicks(Stopwatch.GetTimestamp() - _lastMouseWheelScroll).TotalSeconds > 0.25) { // don't set start position when scrolling with mouse wheel as it will make a bad (jumping back) forward scrolling StartPositionSeconds = startPositionSeconds; } @@ -1195,7 +1196,7 @@ namespace Nikse.SubtitleEdit.Controls _firstMove = true; if (e.Button == MouseButtons.Left) { - _buttonDownTimeTicks = DateTime.UtcNow.Ticks; + _buttonDownTimeTicks = Stopwatch.GetTimestamp(); Cursor = Cursors.VSplit; double seconds = RelativeXPositionToSeconds(e.X); @@ -2086,7 +2087,7 @@ namespace Nikse.SubtitleEdit.Controls if (e.Button == MouseButtons.Left && OnSingleClick != null) { var diff = Math.Abs(_mouseMoveStartX - e.X); - if (_mouseMoveStartX == -1 || _mouseMoveEndX == -1 || diff < 10 && TimeSpan.FromTicks(DateTime.UtcNow.Ticks - _buttonDownTimeTicks).TotalSeconds < 0.25) + if (_mouseMoveStartX == -1 || _mouseMoveEndX == -1 || diff < 10 && TimeSpan.FromTicks(Stopwatch.GetTimestamp() - _buttonDownTimeTicks).TotalSeconds < 0.25) { if (ModifierKeys == Keys.Shift && SelectedParagraph != null) { @@ -2326,7 +2327,7 @@ namespace Nikse.SubtitleEdit.Controls else { StartPositionSeconds += delta / 256.0; - _lastMouseWheelScroll = DateTime.UtcNow.Ticks; + _lastMouseWheelScroll = Stopwatch.GetTimestamp(); if (_currentVideoPositionSeconds < _startPositionSeconds || _currentVideoPositionSeconds >= EndPositionSeconds) { OnPositionSelected?.Invoke(this, new ParagraphEventArgs(_startPositionSeconds, null)); diff --git a/src/ui/Controls/SimpleTextBox.cs b/src/ui/Controls/SimpleTextBox.cs index 6d60a0bc0..d9667e27f 100644 --- a/src/ui/Controls/SimpleTextBox.cs +++ b/src/ui/Controls/SimpleTextBox.cs @@ -1,6 +1,7 @@ using Nikse.SubtitleEdit.Core.Common; using Nikse.SubtitleEdit.Logic; using System; +using System.Diagnostics; using System.Drawing; using System.Windows.Forms; @@ -30,7 +31,7 @@ namespace Nikse.SubtitleEdit.Controls KeyDown += SETextBox_KeyDown; // To fix issue where WM_LBUTTONDOWN got wrong "SelectedText" (only in undocked mode) - GotFocus += (sender, args) => { _gotFocusTicks = DateTime.UtcNow.Ticks; }; + GotFocus += (sender, args) => { _gotFocusTicks = Stopwatch.GetTimestamp(); }; } private void SetAlignment() @@ -208,7 +209,7 @@ namespace Nikse.SubtitleEdit.Controls if (_dragFromThis) { _dragFromThis = false; - var milliseconds = (DateTime.UtcNow.Ticks - _dragStartTicks) / 10000; + var milliseconds = (Stopwatch.GetTimestamp() - _dragStartTicks) / 10000; if (milliseconds < 400) { SelectionLength = 0; @@ -337,12 +338,12 @@ namespace Nikse.SubtitleEdit.Controls if (m.Msg == WM_LBUTTONDOWN) { - var milliseconds = (DateTime.UtcNow.Ticks - _gotFocusTicks) / 10000; + var milliseconds = (Stopwatch.GetTimestamp() - _gotFocusTicks) / 10000; if (milliseconds > 10) { _dragText = SelectedText; _dragStartFrom = SelectionStart; - _dragStartTicks = DateTime.UtcNow.Ticks; + _dragStartTicks = Stopwatch.GetTimestamp(); } } diff --git a/src/ui/Forms/AudioToText/VoskAudioToText.cs b/src/ui/Forms/AudioToText/VoskAudioToText.cs index f4f6c8cee..c416773ad 100644 --- a/src/ui/Forms/AudioToText/VoskAudioToText.cs +++ b/src/ui/Forms/AudioToText/VoskAudioToText.cs @@ -326,7 +326,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText var buffer = new byte[4096]; _bytesWavTotal = new FileInfo(waveFileName).Length; _bytesWavRead = 0; - _startTicks = DateTime.UtcNow.Ticks; + _startTicks = Stopwatch.GetTimestamp(); timer1.Start(); using (var source = File.OpenRead(waveFileName)) { @@ -605,7 +605,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText return; } - var durationMs = (DateTime.UtcNow.Ticks - _startTicks) / 10_000; + var durationMs = (Stopwatch.GetTimestamp() - _startTicks) / 10_000; var msPerFrame = (float)durationMs / _bytesWavRead; var estimatedTotalMs = msPerFrame * _bytesWavTotal; var estimatedLeft = ProgressHelper.ToProgressTime(estimatedTotalMs - durationMs); diff --git a/src/ui/Forms/AudioToText/VoskAudioToTextSelectedLines.cs b/src/ui/Forms/AudioToText/VoskAudioToTextSelectedLines.cs index 41562f3a9..450237a09 100644 --- a/src/ui/Forms/AudioToText/VoskAudioToTextSelectedLines.cs +++ b/src/ui/Forms/AudioToText/VoskAudioToTextSelectedLines.cs @@ -1,5 +1,6 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Linq; using System.Text; @@ -227,7 +228,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText var buffer = new byte[4096]; _bytesWavTotal = new FileInfo(waveFileName).Length; _bytesWavRead = 0; - _startTicks = DateTime.UtcNow.Ticks; + _startTicks = Stopwatch.GetTimestamp(); timer1.Start(); using (var source = File.OpenRead(waveFileName)) { @@ -325,7 +326,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText return; } - var durationMs = (DateTime.UtcNow.Ticks - _startTicks) / 10_000; + var durationMs = (Stopwatch.GetTimestamp() - _startTicks) / 10_000; var msPerFrame = (float)durationMs / _bytesWavRead; var estimatedTotalMs = msPerFrame * _bytesWavTotal; var estimatedLeft = ProgressHelper.ToProgressTime(estimatedTotalMs - durationMs); diff --git a/src/ui/Forms/AudioToText/WhisperAudioToText.cs b/src/ui/Forms/AudioToText/WhisperAudioToText.cs index 1230ec9fd..cc4b000ae 100644 --- a/src/ui/Forms/AudioToText/WhisperAudioToText.cs +++ b/src/ui/Forms/AudioToText/WhisperAudioToText.cs @@ -929,7 +929,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText var process = GetWhisperProcess(inputFile, model.Name, _languageCode, checkBoxTranslateToEnglish.Checked, OutputHandler); var sw = Stopwatch.StartNew(); _outputText.Add($"Calling whisper ({Configuration.Settings.Tools.WhisperChoice}) with : {process.StartInfo.FileName} {process.StartInfo.Arguments}{Environment.NewLine}"); - _startTicks = DateTime.UtcNow.Ticks; + _startTicks = Stopwatch.GetTimestamp(); _videoInfo = UiUtil.GetVideoInfo(waveFileName); timer1.Start(); if (!_batchMode) @@ -1573,7 +1573,7 @@ namespace Nikse.SubtitleEdit.Forms.AudioToText return; } - var durationMs = (DateTime.UtcNow.Ticks - _startTicks) / 10_000; + var durationMs = (Stopwatch.GetTimestamp() - _startTicks) / 10_000; labelElapsed.Text = new TimeCode(durationMs).ToShortDisplayString(); if (_endSeconds <= 0 || _videoInfo == null) diff --git a/src/ui/Forms/GenerateTransparentVideoWithSubtitles.cs b/src/ui/Forms/GenerateTransparentVideoWithSubtitles.cs index acaeedb43..662c36adf 100644 --- a/src/ui/Forms/GenerateTransparentVideoWithSubtitles.cs +++ b/src/ui/Forms/GenerateTransparentVideoWithSubtitles.cs @@ -802,7 +802,7 @@ namespace Nikse.SubtitleEdit.Forms process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); - _startTicks = DateTime.UtcNow.Ticks; + _startTicks = Stopwatch.GetTimestamp(); timer1.Start(); while (!process.HasExited) @@ -855,7 +855,7 @@ namespace Nikse.SubtitleEdit.Forms return; } - var durationMs = (DateTime.UtcNow.Ticks - _startTicks) / 10_000; + var durationMs = (Stopwatch.GetTimestamp() - _startTicks) / 10_000; var msPerFrame = (float)durationMs / _processedFrames; var estimatedTotalMs = msPerFrame * _totalFrames; var estimatedLeft = ProgressHelper.ToProgressTime(estimatedTotalMs - durationMs); diff --git a/src/ui/Forms/GenerateVideo.cs b/src/ui/Forms/GenerateVideo.cs index e889dec84..c002bc7aa 100644 --- a/src/ui/Forms/GenerateVideo.cs +++ b/src/ui/Forms/GenerateVideo.cs @@ -199,7 +199,7 @@ namespace Nikse.SubtitleEdit.Forms process.BeginErrorReadLine(); _totalFrames = (long)Math.Round(float.Parse(comboBoxFrameRate.Text, CultureInfo.CurrentCulture) * (float)numericUpDownDurationMinutes.Value * 60.0f); progressBar1.Maximum = (int)_totalFrames; - _startTicks = DateTime.UtcNow.Ticks; + _startTicks = Stopwatch.GetTimestamp(); timer1.Start(); while (!process.HasExited) { @@ -314,7 +314,7 @@ namespace Nikse.SubtitleEdit.Forms return; } - var durationMs = (DateTime.UtcNow.Ticks - _startTicks) / 10_000; + var durationMs = (Stopwatch.GetTimestamp() - _startTicks) / 10_000; var msPerFrame = (float)durationMs / _processedFrames; var estimatedTotalMs = msPerFrame * _totalFrames; var estimatedLeft = ProgressHelper.ToProgressTime(estimatedTotalMs - durationMs); diff --git a/src/ui/Forms/GenerateVideoWithHardSubs.cs b/src/ui/Forms/GenerateVideoWithHardSubs.cs index d66d4b2f4..a364b2825 100644 --- a/src/ui/Forms/GenerateVideoWithHardSubs.cs +++ b/src/ui/Forms/GenerateVideoWithHardSubs.cs @@ -935,7 +935,7 @@ namespace Nikse.SubtitleEdit.Forms process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); - _startTicks = DateTime.UtcNow.Ticks; + _startTicks = Stopwatch.GetTimestamp(); timer1.Start(); while (!process.HasExited) @@ -965,7 +965,7 @@ namespace Nikse.SubtitleEdit.Forms process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); - _startTicks = DateTime.UtcNow.Ticks; + _startTicks = Stopwatch.GetTimestamp(); timer1.Start(); while (!process.HasExited) @@ -1054,7 +1054,7 @@ namespace Nikse.SubtitleEdit.Forms process.Start(); process.BeginOutputReadLine(); process.BeginErrorReadLine(); - _startTicks = DateTime.UtcNow.Ticks; + _startTicks = Stopwatch.GetTimestamp(); timer1.Start(); while (!process.HasExited) @@ -1185,7 +1185,7 @@ namespace Nikse.SubtitleEdit.Forms return; } - var durationMs = (DateTime.UtcNow.Ticks - _startTicks) / 10_000; + var durationMs = (Stopwatch.GetTimestamp() - _startTicks) / 10_000; var msPerFrame = (float)durationMs / _processedFrames; var estimatedTotalMs = msPerFrame * _totalFrames; var estimatedLeft = ProgressHelper.ToProgressTime(estimatedTotalMs - durationMs); diff --git a/src/ui/Forms/Main.cs b/src/ui/Forms/Main.cs index 7bd115b15..0cab6f10b 100644 --- a/src/ui/Forms/Main.cs +++ b/src/ui/Forms/Main.cs @@ -2456,7 +2456,7 @@ namespace Nikse.SubtitleEdit.Forms private void AutoSave(bool force = false) { if (!Configuration.Settings.General.AutoSave || - DateTime.UtcNow.Ticks - _lastAutoSave < 10000 * 3000 && !force) // only check for auto save evety 3 seconds + Stopwatch.GetTimestamp() - _lastAutoSave < 10000 * 3000 && !force) // only check for auto save evety 3 seconds { return; } @@ -2476,7 +2476,7 @@ namespace Nikse.SubtitleEdit.Forms private void DoAutoSave() { - _lastAutoSave = DateTime.UtcNow.Ticks + 1009000; + _lastAutoSave = Stopwatch.GetTimestamp() + 1009000; var currentSubtitleHash = GetFastSubtitleHash(); if (_changeSubtitleHash != currentSubtitleHash && _lastDoNotPrompt != currentSubtitleHash && _subtitle?.Paragraphs.Count > 0) { @@ -2493,7 +2493,7 @@ namespace Nikse.SubtitleEdit.Forms SaveOriginalSubtitle(GetCurrentSubtitleFormat(), true); } - _lastAutoSave = DateTime.UtcNow.Ticks; + _lastAutoSave = Stopwatch.GetTimestamp(); } private bool ContinueNewOrExit() @@ -2672,10 +2672,10 @@ namespace Nikse.SubtitleEdit.Forms return; } - if ((DateTime.UtcNow.Ticks - _lastHistoryTicks) > 10000 * 500) // only if last change was longer ago than 500 milliseconds + if ((Stopwatch.GetTimestamp() - _lastHistoryTicks) > 10000 * 500) // only if last change was longer ago than 500 milliseconds { MakeHistoryForUndo(description); - _lastHistoryTicks = DateTime.UtcNow.Ticks; + _lastHistoryTicks = Stopwatch.GetTimestamp(); } } @@ -2887,7 +2887,7 @@ namespace Nikse.SubtitleEdit.Forms return; } - _lastAutoSave = DateTime.UtcNow.Ticks; + _lastAutoSave = Stopwatch.GetTimestamp(); bool videoFileLoaded = false; _formatManuallyChanged = false; var file = new FileInfo(fileName); @@ -6499,7 +6499,7 @@ namespace Nikse.SubtitleEdit.Forms ShowSourceLineNumber(); _sourceViewChange = true; labelStatus.Text = string.Empty; - _sourceTextTicks = DateTime.UtcNow.Ticks; + _sourceTextTicks = Stopwatch.GetTimestamp(); } private bool ShowProfileInStatusBar => Configuration.Settings.General.CurrentProfile != "Default"; @@ -11513,7 +11513,7 @@ namespace Nikse.SubtitleEdit.Forms private void TextBoxListViewTextKeyDown(object sender, KeyEventArgs e) { - _listViewTextTicks = DateTime.UtcNow.Ticks; + _listViewTextTicks = Stopwatch.GetTimestamp(); if (e.Modifiers == Keys.Shift && e.KeyCode == Keys.ShiftKey) { @@ -11810,7 +11810,7 @@ namespace Nikse.SubtitleEdit.Forms } // last key down in text - _lastTextKeyDownTicks = DateTime.UtcNow.Ticks; + _lastTextKeyDownTicks = Stopwatch.GetTimestamp(); UpdatePositionAndTotalLength(labelTextLineTotal, textBoxListViewText); } @@ -15205,7 +15205,7 @@ namespace Nikse.SubtitleEdit.Forms ShowStatus(string.Format("{0}, {1:0}%", statusMessage, _lastProgressPercent)); statusStrip1.Refresh(); TaskbarList.SetProgressValue(Handle, percent, 100); - if (DateTime.UtcNow.Ticks % 10 == 0) + if (Stopwatch.GetTimestamp() % 10 == 0) { Application.DoEvents(); } @@ -16952,15 +16952,15 @@ namespace Nikse.SubtitleEdit.Forms { if (e.KeyCode == Keys.LWin) { - _winLeftDownTicks = DateTime.UtcNow.Ticks; + _winLeftDownTicks = Stopwatch.GetTimestamp(); } if (e.KeyCode == Keys.RWin) { - _winRightDownTicks = DateTime.UtcNow.Ticks; + _winRightDownTicks = Stopwatch.GetTimestamp(); } - if ((DateTime.UtcNow.Ticks - _winLeftDownTicks) <= 10000 * 999 || (DateTime.UtcNow.Ticks - _winRightDownTicks) <= 10000 * 999) // less than 999 ms + if ((Stopwatch.GetTimestamp() - _winLeftDownTicks) <= 10000 * 999 || (Stopwatch.GetTimestamp() - _winRightDownTicks) <= 10000 * 999) // less than 999 ms { // if it's less than one second since Win key was pressed we ignore key (not perfect...) e.SuppressKeyPress = true; @@ -18398,7 +18398,7 @@ namespace Nikse.SubtitleEdit.Forms mediaPlayer.CurrentPosition = startSeconds; UiUtil.ShowSubtitle(_subtitle, mediaPlayer, GetCurrentSubtitleFormat()); mediaPlayer.Play(); - _endSecondsNewPositionTicks = DateTime.UtcNow.Ticks; + _endSecondsNewPositionTicks = Stopwatch.GetTimestamp(); } e.SuppressKeyPress = true; @@ -18414,7 +18414,7 @@ namespace Nikse.SubtitleEdit.Forms mediaPlayer.CurrentPosition = startSeconds; UiUtil.ShowSubtitle(_subtitle, mediaPlayer, GetCurrentSubtitleFormat()); mediaPlayer.Play(); - _endSecondsNewPositionTicks = DateTime.UtcNow.Ticks; + _endSecondsNewPositionTicks = Stopwatch.GetTimestamp(); } e.SuppressKeyPress = true; @@ -21033,7 +21033,7 @@ namespace Nikse.SubtitleEdit.Forms labelNextWord.Text = string.Format(_language.NextX, HtmlUtil.RemoveHtmlTags(next.Text, true).Replace(Environment.NewLine, " ")); labelNextWord.Left = audioVisualizer.Width / 2 - labelNextWord.Width / 2 + audioVisualizer.Left; labelNextWord.Visible = true; - _labelNextTicks = DateTime.UtcNow.Ticks; + _labelNextTicks = Stopwatch.GetTimestamp(); } else { @@ -21231,7 +21231,7 @@ namespace Nikse.SubtitleEdit.Forms } _textHeightResize = splitContainerListViewAndText.Height - splitContainerListViewAndText.SplitterDistance; - _textHeightResizeIgnoreUpdate = DateTime.UtcNow.Ticks; + _textHeightResizeIgnoreUpdate = Stopwatch.GetTimestamp(); mediaPlayer.ShowFullScreenControls(); bool setRedockOnFullscreenEnd = false; @@ -24183,7 +24183,7 @@ namespace Nikse.SubtitleEdit.Forms int index = ShowSubtitle(); if (index != -1 && oldIndex != index && checkBoxSyncListViewWithVideoWhilePlaying.Checked) { - if ((DateTime.UtcNow.Ticks - _lastTextKeyDownTicks) > 10000 * 700) // only if last typed char was entered > 700 milliseconds + if ((Stopwatch.GetTimestamp() - _lastTextKeyDownTicks) > 10000 * 700) // only if last typed char was entered > 700 milliseconds { if (_endSeconds <= 0 || !checkBoxAutoRepeatOn.Checked) { @@ -24205,7 +24205,7 @@ namespace Nikse.SubtitleEdit.Forms mediaPlayer.Pause(); if (_playSelectionIndex < 0) { - if (_endSecondsNewPosition >= 0 && _endSecondsNewPositionTicks > DateTime.UtcNow.Ticks - (10000 * 900)) // 900 ms + if (_endSecondsNewPosition >= 0 && _endSecondsNewPositionTicks > Stopwatch.GetTimestamp() - (10000 * 900)) // 900 ms { mediaPlayer.CurrentPosition = _endSecondsNewPosition; } @@ -24329,7 +24329,7 @@ namespace Nikse.SubtitleEdit.Forms } - if (labelNextWord.Visible && _labelNextTicks + 100000000 < DateTime.UtcNow.Ticks) + if (labelNextWord.Visible && _labelNextTicks + 100000000 < Stopwatch.GetTimestamp()) { labelNextWord.Visible = false; } @@ -24367,7 +24367,7 @@ namespace Nikse.SubtitleEdit.Forms Text = Text.RemoveChar('*').TrimEnd(); } - if (InSourceView && _sourceTextTicks != -1 && (DateTime.UtcNow.Ticks - _sourceTextTicks) > 10000 * 700) // only if last typed char was entered > 700 milliseconds + if (InSourceView && _sourceTextTicks != -1 && (Stopwatch.GetTimestamp() - _sourceTextTicks) > 10000 * 700) // only if last typed char was entered > 700 milliseconds { ReloadFromSourceInSourceView(); _sourceTextTicks = -1; @@ -24651,7 +24651,7 @@ namespace Nikse.SubtitleEdit.Forms } } - _textHeightResizeIgnoreUpdate = DateTime.UtcNow.Ticks; + _textHeightResizeIgnoreUpdate = Stopwatch.GetTimestamp(); SubtitleListview1.AutoSizeAllColumns(this); if (WindowState == FormWindowState.Maximized || @@ -24684,7 +24684,7 @@ namespace Nikse.SubtitleEdit.Forms private void Main_ResizeBegin(object sender, EventArgs e) { - _textHeightResizeIgnoreUpdate = DateTime.UtcNow.Ticks; + _textHeightResizeIgnoreUpdate = Stopwatch.GetTimestamp(); if (_loading) { return; @@ -25697,7 +25697,7 @@ namespace Nikse.SubtitleEdit.Forms { timerAutoContinue.Stop(); - if ((DateTime.UtcNow.Ticks - _lastTextKeyDownTicks) > 10000 * 700) // only if last typed char was entered > 700 milliseconds + if ((Stopwatch.GetTimestamp() - _lastTextKeyDownTicks) > 10000 * 700) // only if last typed char was entered > 700 milliseconds { labelStatus.Text = string.Empty; PlayNext(); @@ -26343,7 +26343,7 @@ namespace Nikse.SubtitleEdit.Forms { try { - _textHeightResizeIgnoreUpdate = DateTime.UtcNow.Ticks; + _textHeightResizeIgnoreUpdate = Stopwatch.GetTimestamp(); _textHeightResize = splitContainerListViewAndText.Height - splitContainerListViewAndText.SplitterDistance; } catch @@ -26411,7 +26411,7 @@ namespace Nikse.SubtitleEdit.Forms SubtitleListview1.Focus(); } - _lastTextKeyDownTicks = DateTime.UtcNow.Ticks; + _lastTextKeyDownTicks = Stopwatch.GetTimestamp(); if (_subtitleOriginal != null && _subtitleOriginal.Paragraphs.Count > 0) { var idx = _subtitleListViewIndex; @@ -29732,7 +29732,7 @@ namespace Nikse.SubtitleEdit.Forms return; } - _textHeightResizeIgnoreUpdate = DateTime.UtcNow.Ticks; + _textHeightResizeIgnoreUpdate = Stopwatch.GetTimestamp(); mediaPlayer.ShowNonFullScreenControls(); SaveUndockedPositions(); @@ -30091,7 +30091,7 @@ namespace Nikse.SubtitleEdit.Forms private void TextBoxListViewTextOriginalKeyDown(object sender, KeyEventArgs e) { - _listViewOriginalTextTicks = DateTime.UtcNow.Ticks; + _listViewOriginalTextTicks = Stopwatch.GetTimestamp(); if (_subtitleOriginal == null || _subtitleOriginal.Paragraphs.Count < 1) { return; @@ -30283,7 +30283,7 @@ namespace Nikse.SubtitleEdit.Forms } // last key down in text - _lastTextKeyDownTicks = DateTime.UtcNow.Ticks; + _lastTextKeyDownTicks = Stopwatch.GetTimestamp(); UpdatePositionAndTotalLength(labelTextOriginalLineTotal, textBoxListViewTextOriginal); } @@ -31619,7 +31619,7 @@ namespace Nikse.SubtitleEdit.Forms return; } - if ((DateTime.UtcNow.Ticks - _listViewTextTicks) > 10000 * 700) // only if last typed char was entered > 700 milliseconds + if ((Stopwatch.GetTimestamp() - _listViewTextTicks) > 10000 * 700) // only if last typed char was entered > 700 milliseconds { var p = _subtitle.GetParagraphOrDefault(index); if (p == null) @@ -31713,7 +31713,7 @@ namespace Nikse.SubtitleEdit.Forms return; } - if ((DateTime.UtcNow.Ticks - _listViewOriginalTextTicks) > 10000 * 700) // only if last typed char was entered > 700 milliseconds + if ((Stopwatch.GetTimestamp() - _listViewOriginalTextTicks) > 10000 * 700) // only if last typed char was entered > 700 milliseconds { var original = Utilities.GetOriginalParagraph(index, _subtitle.Paragraphs[index], _subtitleOriginal.Paragraphs); if (original != null) @@ -34207,7 +34207,7 @@ namespace Nikse.SubtitleEdit.Forms { if (Configuration.Settings.VideoControls.WaveformFocusOnMouseEnter && audioVisualizer.WavePeaks != null && !audioVisualizer.Focused && audioVisualizer.CanFocus) { - if (Math.Abs(_lastWaveformMenuCloseTicks - DateTime.UtcNow.Ticks) > 10000 * 500) // only if last change was longer ago than 500 milliseconds + if (Math.Abs(_lastWaveformMenuCloseTicks - Stopwatch.GetTimestamp()) > 10000 * 500) // only if last change was longer ago than 500 milliseconds { audioVisualizer.Focus(); } @@ -34529,7 +34529,7 @@ namespace Nikse.SubtitleEdit.Forms private void ContextMenuStripWaveformClosing(object sender, ToolStripDropDownClosingEventArgs e) { - _lastWaveformMenuCloseTicks = DateTime.UtcNow.Ticks; + _lastWaveformMenuCloseTicks = Stopwatch.GetTimestamp(); } private void MenuOpened(object sender, EventArgs e) @@ -35603,7 +35603,7 @@ namespace Nikse.SubtitleEdit.Forms } } - var diff = DateTime.UtcNow.Ticks - _textHeightResizeIgnoreUpdate; + var diff = Stopwatch.GetTimestamp() - _textHeightResizeIgnoreUpdate; if (diff > 10_000 * 750 && // 750 ms WindowState == _lastFormWindowState) { @@ -35818,7 +35818,7 @@ namespace Nikse.SubtitleEdit.Forms private void SplitContainerMainSplitterMoving(object sender, SplitterCancelEventArgs e) { - _textHeightResizeIgnoreUpdate = DateTime.UtcNow.Ticks; + _textHeightResizeIgnoreUpdate = Stopwatch.GetTimestamp(); } private void ApplyCustomStylesToolStripMenuItemClick(object sender, EventArgs e) diff --git a/src/ui/Forms/Ocr/VobSubOcr.cs b/src/ui/Forms/Ocr/VobSubOcr.cs index d1a43a40e..5c988cb06 100644 --- a/src/ui/Forms/Ocr/VobSubOcr.cs +++ b/src/ui/Forms/Ocr/VobSubOcr.cs @@ -32,6 +32,7 @@ using Nikse.SubtitleEdit.Core.Settings; using MessageBox = Nikse.SubtitleEdit.Forms.SeMsgBox.MessageBox; using Timer = System.Windows.Forms.Timer; using System.Runtime.CompilerServices; +using System.Diagnostics; namespace Nikse.SubtitleEdit.Forms.Ocr { @@ -6645,7 +6646,7 @@ namespace Nikse.SubtitleEdit.Forms.Ocr { if (subtitleListView1.SelectedItems.Count > 1) { - if (DateTime.UtcNow.Ticks - _slvSelIdxChangedTicks < 100000) + if (Stopwatch.GetTimestamp() - _slvSelIdxChangedTicks < 100000) { _slvSelIdx = new KeyValuePair(_slvSelIdxChangedTicks, subtitleListView1.SelectedItems[0].Index); if (_changeDelayTimer == null) @@ -6659,10 +6660,10 @@ namespace Nikse.SubtitleEdit.Forms.Ocr _changeDelayTimer.Stop(); _changeDelayTimer.Start(); } - _slvSelIdxChangedTicks = DateTime.UtcNow.Ticks; + _slvSelIdxChangedTicks = Stopwatch.GetTimestamp(); return; } - _slvSelIdxChangedTicks = DateTime.UtcNow.Ticks; + _slvSelIdxChangedTicks = Stopwatch.GetTimestamp(); } _selectedIndex = subtitleListView1.SelectedItems[0].Index; } diff --git a/src/ui/Forms/Options/WordLists.cs b/src/ui/Forms/Options/WordLists.cs index 40d2039a5..3425765b5 100644 --- a/src/ui/Forms/Options/WordLists.cs +++ b/src/ui/Forms/Options/WordLists.cs @@ -3,6 +3,7 @@ using Nikse.SubtitleEdit.Core.Dictionaries; using Nikse.SubtitleEdit.Logic; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Globalization; using System.IO; using System.Linq; @@ -694,7 +695,7 @@ namespace Nikse.SubtitleEdit.Forms.Options e.SuppressKeyPress = true; if (TimeSpan.FromTicks(_listBoxSearchStringLastUsed.Ticks).TotalMilliseconds + 1800 < - TimeSpan.FromTicks(DateTime.UtcNow.Ticks).TotalMilliseconds) + TimeSpan.FromTicks(Stopwatch.GetTimestamp()).TotalMilliseconds) { _listBoxSearchString = string.Empty; } @@ -866,7 +867,7 @@ namespace Nikse.SubtitleEdit.Forms.Options e.SuppressKeyPress = true; if (TimeSpan.FromTicks(_listBoxSearchStringLastUsed.Ticks).TotalMilliseconds + 1800 < - TimeSpan.FromTicks(DateTime.UtcNow.Ticks).TotalMilliseconds) + TimeSpan.FromTicks(Stopwatch.GetTimestamp()).TotalMilliseconds) { _listBoxSearchString = string.Empty; } @@ -902,7 +903,7 @@ namespace Nikse.SubtitleEdit.Forms.Options e.SuppressKeyPress = true; if (TimeSpan.FromTicks(_listBoxSearchStringLastUsed.Ticks).TotalMilliseconds + 1800 < - TimeSpan.FromTicks(DateTime.UtcNow.Ticks).TotalMilliseconds) + TimeSpan.FromTicks(Stopwatch.GetTimestamp()).TotalMilliseconds) { _listBoxSearchString = string.Empty; } diff --git a/src/ui/Logic/UiUtil.cs b/src/ui/Logic/UiUtil.cs index 18be2a3ea..589da70e2 100644 --- a/src/ui/Logic/UiUtil.cs +++ b/src/ui/Logic/UiUtil.cs @@ -67,7 +67,7 @@ namespace Nikse.SubtitleEdit.Logic return LibMpvDynamic.GetVideoInfo(fileName); } - private static long _lastShowSubTicks = DateTime.UtcNow.Ticks; + private static long _lastShowSubTicks = Stopwatch.GetTimestamp(); private static int _lastShowSubHash; public static int ShowSubtitle(Subtitle subtitle, VideoPlayerContainer videoPlayerContainer, SubtitleFormat format) @@ -115,7 +115,7 @@ namespace Nikse.SubtitleEdit.Logic private static void TimeOutRefresh(Subtitle subtitle, VideoPlayerContainer videoPlayerContainer, SubtitleFormat format, Paragraph p = null) { - if (DateTime.UtcNow.Ticks - _lastShowSubTicks > 10000 * 1000) // more than 1+ seconds ago + if (Stopwatch.GetTimestamp() - _lastShowSubTicks > 10000 * 1000) // more than 1+ seconds ago { var newHash = subtitle.GetFastHashCode(string.Empty); if (newHash != _lastShowSubHash) @@ -124,7 +124,7 @@ namespace Nikse.SubtitleEdit.Logic _lastShowSubHash = newHash; } - _lastShowSubTicks = DateTime.UtcNow.Ticks; + _lastShowSubTicks = Stopwatch.GetTimestamp(); } } diff --git a/src/ui/Logic/WindowsHelper.cs b/src/ui/Logic/WindowsHelper.cs index e2b839e87..92b6e862a 100644 --- a/src/ui/Logic/WindowsHelper.cs +++ b/src/ui/Logic/WindowsHelper.cs @@ -1,4 +1,4 @@ -using System; +using System.Diagnostics; using Nikse.SubtitleEdit.Core.Common; namespace Nikse.SubtitleEdit.Logic @@ -14,10 +14,10 @@ namespace Nikse.SubtitleEdit.Logic return; } - if (DateTime.UtcNow.Ticks - _lastCall > 10_000_000 * 60) // one minute + if (Stopwatch.GetTimestamp() - _lastCall > 10_000_000 * 60) // one minute { NativeMethods.SetThreadExecutionState(NativeMethods.ES_CONTINUOUS | NativeMethods.ES_SYSTEM_REQUIRED); - _lastCall = DateTime.UtcNow.Ticks; + _lastCall = Stopwatch.GetTimestamp(); } } }