2

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.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
jmwierzbicki
  • 177
  • 9
  • 1
    when you say it returns you a NullReferenceException - at which line does this occur? – Slugart Aug 15 '13 at 21:13
  • It is whole invoke block at ServerProc_ErrorDataReceived – jmwierzbicki Aug 15 '13 at 21:21
  • 1
    What is null, e? e.Data? – Slugart Aug 15 '13 at 21:25
  • You can add a breakpoint at the line JUST after `Invoke` (the line which contains the opening bracket), and VS should catch that. Then you can check which variable is `null`. My guess is it might be `e.Data`... – MBender Aug 15 '13 at 21:27
  • after some experiments i found that problem was caused by code that i decided to strip out of application: still i have no idea why the program acts in such way. the problem is caused by block if (e.Data.Contains("nastything")) (code opened in first post) – jmwierzbicki Aug 15 '13 at 21:34
  • Never use Invoke(), too many odds for deadlock. Always use BeginInvoke(). – Hans Passant Aug 15 '13 at 21:38
  • VS automatically put break point in debug mode - the problem is that message it throws is stupidly translated, and i really have no idea how to spell it in english corectly this is how it looks like when i kill java application http://scr.hu/0hc8/f3kwi – jmwierzbicki Aug 15 '13 at 21:43
  • after replacing Invoke() with BeginInvoke() my application closes along with killed java program, and VS highlight Application.Run(new Form1()); and program closes itself, throwing Exception has been thrown by the target of an invocation. Still no idea how to repair that – jmwierzbicki Aug 15 '13 at 21:58
  • 2
    Welcome to Stack Overflow! Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Aug 15 '13 at 22:08
  • Thank You John Saunders for Your reply, i wrapped problematic code with if (e.Data != null) and it apparently stopped causing exception - but still, i cant understand why the whole thing happened - i understand that in time i kill java program, e.Data becomes null, but it should not matter since ServerProc_ErrorDataReceived event isn't called any more? – jmwierzbicki Aug 15 '13 at 22:18

1 Answers1

0

Can you just use check if e.Data!=null?

private void ServerProc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
    Invoke(new Action(() =>
    {
        if (e.Data!=null && e.Data.Contains("nastything"))
        {
            System.Windows.Forms.MessageBox.Show("Something nasty happened in console ");
        }
        if (e.Data!=null) f2.richTextBox1.AppendText(e.Data + "\n");
    }));
}

Or use try-catch.

milanio
  • 4,082
  • 24
  • 34