c# - How to queue up system process start -
i've written following code loop around load of data table rows , generate pdf if doesn't exist. works, launches wkhtmltopdfs in 1 go 30 processes started , kill server, d'oh!
what's best way launch 1 process @ time , not start second until previous 1 has run?
db db = new db(); datatable dtposters = db.getdatatable("select * invoices pdfgenerated <> 1 or pdfgenerated null"); foreach (datarow dr in dtposters.rows) { //generate poster, if exsists (don't!) directory.createdirectory(string.format("d:\\invoices\\")); string filepath = string.format("d:\\invoices\\{0}.pdf", dr["invoicenumber"].tostring()); if (!file.exists(filepath)) { string cmd = string.format("c:\\services\\automafe\\wkhtmltopdf.exe http://invoicegen/viewinvoice.aspx?invoicenumber={0} {1}", dr["invoicenumber"].tostring(), filepath); system.diagnostics.process.start("cmd", "/c " + cmd); } else { //update db } }
process.start returns process object, lets monitor state of newly-created process.
a simple, blocking way have each worker wait process exit
var myprocess = system.diagnostics.process.start("cmd", "/c " + cmd); myprocess.waitforexit();
a better way use exited event handler monitor when process exits.
var myprocess = system.diagnostics.process.start("cmd", "/c " + cmd); myprocess.exited += myprocessfinishedhandler;
Comments
Post a Comment