I'm trying to make a C# application that acts as a Java application wrapper.
One of the functions I'm trying to implement is to redirect console output to my program, unfortunately, when I kill my Java process, the whole program freezes.
Here is the code:
private void button1_Click(object sender, EventArgs e)
{
String start = @"myjavaapp.jar";
var startInfo = new ProcessStartInfo("java", start);
startInfo.RedirectStandardInput = startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
startInfo.CreateNoWindow = true;
Process ServerProc = new Process();
ServerProc.StartInfo = startInfo;
ServerProc.EnableRaisingEvents = true;
ServerProc.ErrorDataReceived += new DataReceivedEventHandler(ServerProc_ErrorDataReceived);
ServerProc.Exited += new EventHandler(ServerProc_Exited);
ServerProc.Start();
ServerProc.BeginErrorReadLine();
}
private void ServerProc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
Invoke(new Action(() =>
{
if (e.Data.Contains("nastything"))
{
System.Windows.Forms.MessageBox.Show("Something nasty happened in console ");
}
f2.richTextBox1.AppendText(e.Data + "\n");
}));
}
I think there is something wrong with my thread managing - maybe I don't understand it yet. Anyway, in debug mode vs2012 returns me a NullReferenceException
.