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();
}
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.