C# Display standard output of the child process in the textbox while process is working -


i have form textbox output_txtb , want run cmd, execute commands , display standard output in textbox. want retrieve standard output after execute each command, while child process working. i've found solutions here:

redirect output (stdout, stderr) of child process output window in visual studio

c# process output while running

but didn't solve specific problem, because want display standard output in textbox (not in console or elsewhere) , after execute each command - don't want retrieve full output after process exit.

i've tried use outputdatareceived event in child process, suggested in above links, there problem, when want refer textbox, created on thread - throws invalidoperationexception. here code:

        process process = new process();         process.enableraisingevents = true;          process.startinfo.filename = "cmd";         process.startinfo.createnowindow = true;         process.startinfo.useshellexecute = false;         process.startinfo.redirectstandardoutput = true;         process.startinfo.redirectstandardinput = true;         process.outputdatareceived += (sender, args) =>             {                 //here throws exception                 output_txtb.appendtext(args.data + environment.newline);             };          process.start();         process.beginoutputreadline();          //commands want execute         process.standardinput.writeline("example.exe");         process.standardinput.writeline("script.bat");         process.standardinput.writeline("ipconfig /all");         process.standardinput.writeline("dir");         process.standardinput.close();          process.waitforexit(); 

additional information of exception is:

cross-thread operation not valid: control 'output_txtb' accessed thread other thread created on.

any ideas, how retrieve standard output of child process, , display in textbox, while child process working?

edit: see "example.exe" code:

        console.writeline("line 1");         system.threading.thread.sleep(2000);         console.writeline("line 2");         system.threading.thread.sleep(2000);         console.writeline("line 3");         system.threading.thread.sleep(2000);         console.writeline("line 4");         system.threading.thread.sleep(2000);         console.writeline("line 5"); 

what i'm trying achieve, display in textbox line x each time, process receives in standard output. if use stefano's solution, seems outputdatareceived event fires , displays full process output after process exit.

you should use invoke().

within different thread, cannot update views. logic modify views should done on ui thread, , invoke() used this.

most have thread in background runs , reads output process. thread, if want update ui, use control.invoke() method such below.

i prefer syntax because how "natural" is:

    mycontrol.invoke((action)delegate     {         //you can put ui update logic here         mycontrol.text = "hello world different thread";     }); 

additionally, not need call invoke directly on control being modified. calling invoke in control (or directly on form) mean code within run on ui thread means can update other ui, too.

    mycontrol.invoke((action)delegate     {         //you can put ui update logic here         mycontrol.text = "hello world different thread";         myothercontrol.text = "look can update other controls";     });      myform.invoke((action)delegate     {         mycontrol.text = "can call invoke form";     } 

Comments