c# - NullReferenceException while handling ErrorDataReceived - killed thread -
i'm trying make c# application acts java application wrapper.
one of functions i'm trying implement redirect console output program, unfortunately, when kill java process, whole program freezes.
here 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 wrong thread managing - maybe don't understand yet. anyway, in debug mode vs2012 returns me nullreferenceexception
.
can 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.
Comments
Post a Comment