Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async support for video generation process ✨ #8916

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 26 additions & 14 deletions src/ui/Forms/GenerateVideoWithSoftSubs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MessageBox = Nikse.SubtitleEdit.Forms.SeMsgBox.MessageBox;

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;
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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 every 30 seconds
if (timeSpent++ % 30 == 0)
{
WindowsHelper.PreventStandBy();
}

if (_abort)
{
process.Kill();
break;
}
}
}

process.Dispose();
}).ConfigureAwait(true);
}
finally
{
Expand Down