0

There is a way to prevent a user to close 7za.exe window during his process? I need to show a progression of a folder extraction but if a user close the window, this could cause some errors in my C# program.

public partial class ExtractForm : Form
{
    public ExtractForm()
    {
        InitializeComponent();
    }

    private void ExtractForm_Load(object sender, EventArgs e)
    {
        InitializeEvent();
    }

    private void InitializeEvent()
    {
        Zip.LogFileExtract +=WriteExtractProgression;
    }

    private void WriteExtractProgression(string text)
    {
        if (InvokeRequired)
        {
            this.BeginInvoke(new Action<string>(WriteExtractProgression), text);
        }
        else
        {
            txtExtract.Text += text;
            txtExtract.SelectionStart = txtExtract.TextLength;
            txtExtract.ScrollToCaret();
            txtExtract.Refresh();
        }
    }
}

Process method:

ExtractForm extractForm = new ExtractForm();
extractForm.Show();

Process zipProcess = new Process();
        using (zipProcess)
        {
            zipProcess.StartInfo.UseShellExecute = false;            //Show the cmd.
            zipProcess.StartInfo.RedirectStandardOutput = true;
            zipProcess.OutputDataReceived += (object sender, DataReceivedEventArgs outline) =>
            {
                LogFileExtract(outline.Data);
                // Add args to a TextBox, ListBox, or other UI element
            };
            zipProcess.StartInfo.CreateNoWindow = true;
            zipProcess.StartInfo.FileName = pathToZip;
            zipProcess.StartInfo.Arguments = args;
            zipProcess.Start();
            zipProcess.BeginOutputReadLine();
            zipProcess.WaitForExit();    //Wait the process to finish completely.

        }
        extractForm.Close();
    } 

1 Answers1

1

There is no direct way to prevent the closing of a external window, which the console window is, even if you started it.

For this specific use case, you can capture the output of the Process you started using something like:

process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.OutputDataReceived += (sender, args) => 
{
    // Add args to a TextBox, ListBox, or other UI element
}
process.Start();
process.BeginOutputReadLine();

That will give you direct control over the UI elements. As a bonus, there's no chance the console window will get lost behind your application while it's running.

Anon Coward
  • 9,784
  • 3
  • 26
  • 37
  • Yeah but the process can last several minutes and if I don't show the console window, the customer will ask him if something goes wrong – Émile Pettersen-Coulombe Jul 29 '16 at 12:06
  • Really, the customer won't be ok with seeing the output of the process in a window you control? You can even make the TextBox look vaguely like a console window if you like. – Anon Coward Jul 29 '16 at 12:51
  • The best would be to make a progress bar but I don't think I can with 7za program. With your code i'm able to get the progress and write in my winform like I see. It's the best I can't do probably. Thank you :) – Émile Pettersen-Coulombe Jul 29 '16 at 15:42
  • But I'm unable to capture the output and write it in a textbox during the process. See my code in the edited question – Émile Pettersen-Coulombe Jul 29 '16 at 16:25