c# - Cancel all tasks if application runs longer than x -
i want stop running tasks if application exceeds runtime. tokensource.cancel() being set tasks still running till end normally. guess due starting process runs batch within cmd / executable file. need kill process, can accomplish tpl, or different route beneficial? read thread.abort() seemed rather unpopular.
i starting background worker main window executes executedefault.
background worker = new backgroundworker(); worker.dowork += (o, ea) => { executedefault(worker, tokensource);};
within execute default access execute function osobject. neccassary tasks started within function. want cancel tasks if application running longer 350000ms.
private void executedefault(backgroundworker worker, cancellationtokensource tokensource) { ... random stuff osobject.runsystemcommands(outputpath, execpath, timeframe, tasks, worker, tokensource); ... random stuff task.waitall(tasks.toarray(), 350000); tokensource.cancel(); }
within here start different processes. either batch execution \c logicaldisk or exe files handle.exe being executed.
aublic static void runsystemcommands(string outputpath, string execpath, int timeframe, list<task> tasks, backgroundowrker worker, cancellationtokensource tokensource) { foreach(command activecommand in commands.systemcommands) { string keyname = activecommmand.name; if (keyname == "foo") { task newtask = task.factory.startnew(() => { executes.executebatch(outputfile, activecommand) }, tokensource.token); tasks.add(newtask); } else if (keyname == "bar") { ... } } }
this how batch commands executed.
public static void executebatch(string outputfile, command command, bool parse = false) { log.debugformat("executing task: {0} | name: {1} | command: {2}", task.currentid, command.name, command.execute); log.info("executing task: " + command.name); //create new process , write process start information process p = new process(); system.diagnostics.processstartinfo startinfo = new system.diagnostics.processstartinfo(); startinfo.windowstyle = system.diagnostics.processwindowstyle.hidden; startinfo.useshellexecute = false; startinfo.filename = @"c:\windows\system32\cmd.exe"; startinfo.standardoutputencoding = encoding.getencoding(850); startinfo.redirectstandardoutput = true; startinfo.redirectstandarderror = true; startinfo.createnowindow = true; startinfo.arguments = command.execute; p.startinfo = startinfo; string delimiter = new string('=', 80); string output = ""; string erroroutput = ""; log.debugformat("task {0}: writing outputfile {1}", task.currentid, outputfile); system.io.file.writealltext(outputfile, (command.execute.replace("/c ", "") + environment.newline)); system.io.file.appendalltext(outputfile, delimiter + environment.newline + environment.newline); p.errordatareceived += (sender, eventargs) => { erroroutput = "###erroroutput### "+ eventargs.data + environment.newline; system.io.file.appendalltext(outputfile, erroroutput); }; p.outputdatareceived += (sender, args) => { if (args==null || args.data==null || args.data == "") return; output =args.data+environment.newline; if (parse == true) { output = parser.parseoutput(command.name, output); } try { system.io.file.appendalltext(outputfile, output); } catch (exception e) { log.debug("could not write file. " + e.message); } }; p.start(); p.beginoutputreadline(); p.waitforexit(command.timeout) log.debugformat("task: {3} | name: {4} | start: {0}, end: {1}, elapsed: {2}", p.starttime.tostring("hh:mm:ss.fff"), datetime.now.tostring("hh:mm:ss.fff"), (datetime.now.subtract(p.starttime)).totalmilliseconds, task.currentid, command.name); }
Comments
Post a Comment