c# - Dispatcher.beginInvoke not executing immediately -
below code far, having issue when call dispatcher.begininvoke, not process these messages @ correct time
class script:
public void execute() { var process = new process(); var startinfo = new processstartinfo("cmd.exe", @"/c c:\test\my.bat"); startinfo.workingdirectory = "c:\\test"; startinfo.redirectstandardoutput = true; startinfo.redirectstandarderror = true; startinfo.useshellexecute = false; startinfo.createnowindow = true; process.enableraisingevents = true; process.startinfo = startinfo; process.outputdatareceived += (sender, args) => outputdatareceived(args.data); process.errordatareceived += (sender, args) => errordatareceived(args.data); process.exited += exited; process.start(); process.beginoutputreadline(); process.beginerrorreadline(); process.waitforexit(); int exitcode = process.exitcode; } public void outputdatareceived(string data) { logging.logger.log("data received in script - " + data); // throw event if have subscriber, else return if (onscriptoutput == null) return; allformattedoutput += environment.newline; allformattedoutput += data; allrawoutput += data; scriptoutputeventargs args = new scriptoutputeventargs(data); onscriptoutput(this, args); }
wpf window calls script class , subscribes onscriptoutput event
the problem below, updateoutputtextbox gets called after script finished, updateoutputtextbox messages processed @ once, not processed when begininvoke called causing screen updated @ end instead of when new output data received.. appreciated!
private void btnrunscript_click(object sender, routedeventargs e) { script script = new script(); script.onscriptoutput += script_onscriptoutput; script.execute(); } private void script_onscriptoutput(object sender, scriptoutputeventargs args) { application.current.dispatcher.begininvoke(new action(() => updateoutputtextbox(args.data)),system.windows.threading.dispatcherpriority.send); logging.logger.log("data received in event "); } private void updateoutputtextbox(string data) { logging.logger.log("data received in update "+data); tboutput.text += environment.newline; tboutput.text += data; }
you calling execute
on ui thread , blocking thread waitforexit
. of begininvoke
actions getting queued up. remove call waitforexit
. if need exit code, value in exited
event handler.
Comments
Post a Comment