From 23d2107b0e2613b82de1b57abec129310a9f8a91 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Sat, 19 Oct 2024 00:14:40 +0100 Subject: [PATCH 1/2] Add async support for video generation process Modified the buttonGenerate_Click and RunEmbedding methods to support asynchronous operations. This change ensures the application remains responsive during lengthy video processing tasks, improving user experience by leveraging async/await and background tasks. Signed-off-by: Ivandro Jao --- src/ui/Forms/GenerateVideoWithSoftSubs.cs | 40 +++++++++++++++-------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/src/ui/Forms/GenerateVideoWithSoftSubs.cs b/src/ui/Forms/GenerateVideoWithSoftSubs.cs index 73d79adc3f..4f7282f353 100644 --- a/src/ui/Forms/GenerateVideoWithSoftSubs.cs +++ b/src/ui/Forms/GenerateVideoWithSoftSubs.cs @@ -13,6 +13,7 @@ using System.IO; using System.Linq; using System.Text; +using System.Threading.Tasks; using System.Windows.Forms; using MessageBox = Nikse.SubtitleEdit.Forms.SeMsgBox.MessageBox; @@ -20,7 +21,7 @@ namespace Nikse.SubtitleEdit.Forms { public sealed partial class GenerateVideoWithSoftSubs : Form { - private bool _abort; + private volatile bool _abort; private readonly Subtitle _subtitle; private VideoInfo _videoInfo; private string _inputVideoFileName; @@ -434,7 +435,7 @@ private void OutputHandler(object sendingProcess, DataReceivedEventArgs outLine) _log?.AppendLine(outLine.Data); } - private void buttonGenerate_Click(object sender, EventArgs e) + private async void buttonGenerate_Click(object sender, EventArgs e) { if (_softSubs.Count == 0) { @@ -499,7 +500,7 @@ private void buttonGenerate_Click(object sender, EventArgs e) var stopWatch = Stopwatch.StartNew(); - RunEmbedding(); + await RunEmbedding(); MillisecondsEncoding = stopWatch.ElapsedMilliseconds; groupBoxSettings.Enabled = true; @@ -592,7 +593,7 @@ private string SuggestNewVideoFileName() return fileName; } - private void RunEmbedding() + private async Task RunEmbedding() { var process = GetFfmpegProcess(_inputVideoFileName, VideoFileName); _log.AppendLine("ffmpeg arguments: " + process.StartInfo.Arguments); @@ -607,20 +608,31 @@ private void RunEmbedding() labelNotSupported.Text = string.Empty; labelPleaseWait.Text = LanguageSettings.Current.General.PleaseWait; Cursor = Cursors.WaitCursor; - process.Start(); - process.BeginOutputReadLine(); - process.BeginErrorReadLine(); - while (!process.HasExited) + await Task.Run(() => { - Application.DoEvents(); - WindowsHelper.PreventStandBy(); - System.Threading.Thread.Sleep(100); - if (_abort) + process.Start(); + process.BeginOutputReadLine(); + process.BeginErrorReadLine(); + + var timeSpent = 1; // process.ExitTime - DateTime.Now; + while (!process.HasExited && !process.WaitForExit(1000)) { - process.Kill(); + // ensure standby prevention after every 30 seconds + if (timeSpent++ % 30 == 0) + { + WindowsHelper.PreventStandBy(); + } + + if (_abort) + { + process.Kill(); + break; + } } - } + + process.Dispose(); + }).ConfigureAwait(true); } finally { From 1dad8d48f573c41789d1ced2b028173b4c8c42e8 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Sat, 19 Oct 2024 00:17:07 +0100 Subject: [PATCH 2/2] Refactor comment for clarity Clarified comment in the video generation process to correctly convey the frequency of standby prevention, ensuring it happens every 30 seconds rather than after 30 seconds. This improves code readability and helps future maintenance. Signed-off-by: Ivandro Jao --- src/ui/Forms/GenerateVideoWithSoftSubs.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ui/Forms/GenerateVideoWithSoftSubs.cs b/src/ui/Forms/GenerateVideoWithSoftSubs.cs index 4f7282f353..f7995d7d99 100644 --- a/src/ui/Forms/GenerateVideoWithSoftSubs.cs +++ b/src/ui/Forms/GenerateVideoWithSoftSubs.cs @@ -618,7 +618,7 @@ await Task.Run(() => var timeSpent = 1; // process.ExitTime - DateTime.Now; while (!process.HasExited && !process.WaitForExit(1000)) { - // ensure standby prevention after every 30 seconds + // ensure standby prevention every 30 seconds if (timeSpent++ % 30 == 0) { WindowsHelper.PreventStandBy();