c# - Events in BackgroundWorker thread not firing while main thread active -



c# - Events in BackgroundWorker thread not firing while main thread active -

for long, have been trying run external .bat file (calls r script statistical processing), , have console redirect u.i.

i think close, have gotten work have run sizable problem! is: bloody works 1 time main thread has ended (via: return;), , not during thread.sleep, or .waitone() or etc.

here code in main thread.

string batloc = allrg___.rscrbin_loc + "current.bat"; backgroundworker watchboxdworker1 = new backgroundworker(); watchboxdworker1.dowork += frmc.watchboxworker1_watchext; frmc.wbresetevent = new autoresetevent(false); watchboxdworker1.runworkerasync(batloc); //thread.sleep(1000*20); //frmc.wbresetevent.waitone(); return;

note commented out sleep and/or waitone() instructions. if seek , utilize these backgroundworker execute, 'events' update u.i not.

the code in form (frmc above) follows,

public void watchboxworker1_watchext(object sender, doworkeventargs e) { string exeloc = (string) e.argument; string arg1 = exeloc; string arg2 = ""; processstartinfo pstartinfo = new processstartinfo(); pstartinfo.filename = exeloc; pstartinfo.arguments = string.format("\"{0}\" \"{1}\"", arg1, arg2); pstartinfo.workingdirectory = path.getdirectoryname(exeloc); pstartinfo.createnowindow = true; pstartinfo.useshellexecute = false; pstartinfo.redirectstandardinput = true; pstartinfo.redirectstandardoutput = true; pstartinfo.redirectstandarderror = true; process process1 = new process(); process1.enableraisingevents = true; process1.outputdatareceived += new datareceivedeventhandler(wboutputhandler); process1.errordatareceived += new datareceivedeventhandler(wberrorhandler); process1.startinfo = pstartinfo; process1.synchronizingobject = rtbwatchbox; process1.start(); process1.beginoutputreadline(); process1.beginerrorreadline(); process1.standardinput.close(); process1.waitforexit(); wbresetevent.set(); } public void wboutputhandler(object source, datareceivedeventargs outline) { int x = 0; if (!string.isnullorempty(outline.data)) { rtbwatchbox.appendtext(outline.data); } } public void wberrorhandler(object source, datareceivedeventargs outline) { int x = 0; if (!string.isnullorempty(outline.data)) { rtbwatchbox.appendtext(outline.data); } }

my problem --

the wboutputhandler , wberrorhandler fired console updates nicely - when main thread has exited (using return;).... if utilize thread.sleep or .waitone() in main thread pass command backgroundworker (watchboxworker1_watchext), code runs successfully, wboutputhandler , wberrorhandler methods not triggered @ all.

in fact, if thread.sleep(10*1000), external programme starts running planned, 10 seconds pass, when main ui thread exits whole big enormous update @ once.

i don't want have main thread closed, want maintain doing stuff there after worker finished!

[ of course of study happy alternate methods improve approach ]

"help me stack overflow, hope!"

the reply set backgroundworker within backgroundworker, created ui thread. thought quite complicated given reletivly simple requirement of printing console output ui!

i phone call functions ui follows -

private void btinsertbcmodls_click(object sender, eventargs e) { backgroundworker bw = new backgroundworker(); bw.dowork += rc2___scratchpad4.bc_runexistingbcmodel; bw.runworkerasync(this); }

next utilize delegate & invoke method on richtextbox need update thread -

delegate void updatewriteboxcallback(string str); public void wbwritebox(string writestring) { if (!string.isnullorempty(writestring)) { if (rtbwatchbox.invokerequired) { updatewriteboxcallback @ = new updatewriteboxcallback(wbwritebox); this.invoke(at, new object[] { writestring }); } else { // append richtextbox required } } }

then within function utilize backgroundworker run console stuff -

public static void bc_runexistingbcmodel(object sender, doworkeventargs e) { rc2___rhinegoldcoreform frmc = e.argument rc2___rhinegoldcoreform; backgroundworker watchboxworker = new backgroundworker(); watchboxworker.dowork += frmc.watchboxworker_runproc; watchboxworker.runworkerasync(batloc); while (watchboxworker.isbusy) thread.sleep(50); frmc.updatergcorestatusbox4("executed script " + m + "... "); }

which in turn, in dowork function, calls wbwritebox function above.

public void watchboxworker_runproc(object sender, doworkeventargs e) { string exeloc = (string) e.argument; string arg1 = exeloc; string arg2 = ""; processstartinfo pstartinfo = new processstartinfo(); pstartinfo.filename = exeloc; pstartinfo.arguments = string.format("\"{0}\" \"{1}\"", arg1, arg2); pstartinfo.workingdirectory = path.getdirectoryname(exeloc); pstartinfo.createnowindow = true; pstartinfo.useshellexecute = false; pstartinfo.redirectstandardinput = true; pstartinfo.redirectstandardoutput = true; pstartinfo.redirectstandarderror = true; process process1 = new process(); process1.enableraisingevents = true; process1.outputdatareceived += (s, e1) => this.wbwritebox(e1.data); process1.errordatareceived += (s, e1) => this.wbwritebox(e1.data); process1.startinfo = pstartinfo; process1.synchronizingobject = rtbwatchbox; process1.start(); process1.beginoutputreadline(); process1.beginerrorreadline(); process1.standardinput.close(); process1.waitforexit(); //wbresetevent.set(); }

phew! tricky solution defined problem. if has improve way, allow me know. , carsten help - magnificent.

c# multithreading process backgroundworker

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -