c# - Windows Form run external process without blocking UI -
i want to:
- show form textbox.
- run external program (notepad.exe ease of example).
- continue allow user enter data form textbox whilst notepad running.
- run more (continue) native form code when notepad closes. update form, amongst other things.
i'm having problems making happen. i'm aware of multitude of posts similar issue, haven't found solution works me.
i have tried:
- doing waitforexit, of course blocks ui , users cannot enter data.
- attempting asynchronous process call, method called when process completed. causes problem new method called thread , can't update form.
- doing wait/sleep loop in ui, again naturally block ui.
what neatest, , simplest solution simple windows form program? there no classes used, , code in form1 class.
the process
class fires exited
event when process exits. can add handler event execute code when process exits without blocking ui thread:
process.enableraisingevents = true; process.exited += (s, args) => dostuff();
alternatively create task
represents completion of process leverage tpl asynchrony:
public static task whenexited(this process process) { var tcs = new taskcompletionsource<bool>(); process.enableraisingevents = true; process.exited += (s, args) => tcs.trysetresult(true); return tcs.task; }
this allow write:
await process.whenexited(); updateui();
Comments
Post a Comment