multithreading - C# Process - Pause or sleep before completion -
i have process:
process pr = new process(); pr.startinfo.filename = @"wput.exe"; pr.startinfo.arguments = @"c:\downloads\ ftp://user:dvm@172.29.200.158/transfer/updates/"; pr.startinfo.redirectstandardoutput = true; pr.startinfo.useshellexecute = false; pr.startinfo. pr.start(); string output = pr.standardoutput.readtoend(); console.writeline("output:"); console.writeline(output);
wput ftp upload client.
at moment when run process , begin upload, app freezes , console output won't show until end. guess first problem solvable using thread.
what want start upload, have pause every often, read whatever output has been generated (use data make progress bar etc), , begin again.
what classes/methods should looking into?
you can use outputdatareceived
event print output asynchronously. there few requirements work:
the event enabled during asynchronous read operations on standardoutput. start asynchronous read operations, must redirect standardoutput stream of process, add event handler outputdatareceived event, , call beginoutputreadline. thereafter, outputdatareceived event signals each time process writes line redirected standardoutput stream, until process exits or calls canceloutputread.
an example of working below. it's doing long running operation has output (findstr /lipsn foo *
on c:\ -- "foo" in file on c drive). start
, beginoutputreadline
calls non-blocking, can other things while console output ftp application rolls in.
if ever want stop reading console, use canceloutputread
/cancelerrorread
methods. also, in example below, i'm handling both standard output , error output single event handler, can separate them , deal them differently if needed.
using system; using system.diagnostics; namespace asyncconsoleread { class program { static void main(string[] args) { process p = new process(); p.startinfo.filename = "findstr.exe"; p.startinfo.arguments = "/lipsn foo *"; p.startinfo.workingdirectory = "c:\\"; p.startinfo.useshellexecute = false; p.startinfo.redirectstandardoutput = true; p.startinfo.redirectstandarderror = true; p.outputdatareceived += new datareceivedeventhandler(ondatareceived); p.errordatareceived += new datareceivedeventhandler(ondatareceived); p.start(); p.beginoutputreadline(); p.waitforexit(); } static void ondatareceived(object sender, datareceivedeventargs e) { console.writeline(e.data); } } }
Comments
Post a Comment