c# - how wait thread with parameters and doesn't freeze main thread -
i write code, call thread parameters. program windows forms. how change code, wait thread, , gui of program not freeze?
var t=new thread(()=>somemethod(someparameter)); t.start(); //t.wait?
if don't have await
available, simplest solution use backgroundworker
:
var bw = new backgroundworker(); bw.dowork += (sender, args) => { // lengthy stuff here -- happen in separate thread ... } bw.runworkercompleted += (sender, args) => { if (args.error != null) // if exception occurred during dowork, messagebox.show(args.error.tostring()); // error handling here // put here stuff wanted put after `t.wait` ... } bw.runworkerasync(); // start background worker
runworkercompleted
runs in ui thread, able update ui there (as opposed stuff happening in dowork
).
Comments
Post a Comment