I am making form application, that also runs a console process on different thread. Basically I need to unblock a button after application has exited. Before I made event handler the process after completion just stopped, but now, after event the applications itself is killed.
Here is the code for making process run:
public void CallConsole()//This Calls the console application
{
Thread.CurrentThread.IsBackground = true;
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.FileName = filename;
if (checkBox1.Checked)
p.StartInfo.CreateNoWindow = true;
p.EnableRaisingEvents = true;
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.ErrorDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
p.Exited += new EventHandler(p_Exited);
p.Disposed += new EventHandler(p_Exited);
p.Start();
p.BeginErrorReadLine();
p.BeginOutputReadLine();
}
I tried to work with Thread.IsBackground property, but that didn’t change anything
Here’s the event handler itself:
void p_Exited(object sender, EventArgs e)//Process on exit or disposed will make button1 avalable
{
button1.Enabled = true;
}
Any ideas why the application after adding
p.EnableRaisingEvents = true;
is now killed, not just the process?
The problem here was that
required invocation and did not have any sort of error handling. Once I added another function that checks
button1.InvokeRequiredand in case it does calls itself again trough invocation it worked out great