From 23d2107b0e2613b82de1b57abec129310a9f8a91 Mon Sep 17 00:00:00 2001 From: Ivandro Jao Date: Sat, 19 Oct 2024 00:14:40 +0100 Subject: [PATCH] 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 73d79adc3..4f7282f35 100644 --- a/src/ui/Forms/GenerateVideoWithSoftSubs.cs +++ b/src/ui/Forms/GenerateVideoWithSoftSubs.cs @@ -13,6 +13,7 @@ using System.Globalization; 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 @@ namespace Nikse.SubtitleEdit.Forms _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 @@ namespace Nikse.SubtitleEdit.Forms var stopWatch = Stopwatch.StartNew(); - RunEmbedding(); + await RunEmbedding(); MillisecondsEncoding = stopWatch.ElapsedMilliseconds; groupBoxSettings.Enabled = true; @@ -592,7 +593,7 @@ namespace Nikse.SubtitleEdit.Forms 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 @@ namespace Nikse.SubtitleEdit.Forms 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 {