Skip to content
Closed
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