0
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            try
            {
                button1.Enabled = false;

                await RunPowerShellAsync();
                await RunFfmpegAsync();
                button1.Enabled = true;
                MessageBox.Show("GIF creation complete!", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"An error occurred: {ex.Message}", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private Task RunPowerShellAsync()
        {
            return Task.Run(() =>
            {
                string powershellScript = "Get-ChildItem 'D:\\New folder (76)\\Image*.png' | ForEach-Object { \"file '$($_.FullName)'\" } > image_list.txt";
                ProcessStartInfo psi = new ProcessStartInfo
                {
                    FileName = "powershell.exe",
                    Arguments = $"-Command \"{powershellScript}\"",
                    UseShellExecute = false,
                    CreateNoWindow = true,
                    RedirectStandardOutput = true,
                    RedirectStandardError = true
                };

                using (Process process = new Process { StartInfo = psi })
                {
                    process.Start();
                    process.WaitForExit();
                }
            });
        }

It's getting to the WaitForExit, but then after that line, nothing happens - it never finishes.

2

1 Answer 1

0

"it never finishes" because WaitForExit waits for powershell to close.

Adding exit to the command, like:

Get-ChildItem 'D:\\New folder (76)\\Image*.png' | ForEach-Object { \"file '$($_.FullName)'\" } > image_list.txt; exit

should do that

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.