c# - Show a form while BackgroundWorker is running -
i want display "loading form" (a form text message plus progressbar style set marquee) while backgroundworker
's job isn't done. when backgroundworker
done, loading form must closed automatically. although use backgroundworker
, main thread should wait until it's done. able using autoresetevent
noticied block main thread, form loading's progressbar freezed too.
my question is: how can show form without freeze while runing process in background , wait finish? hope it's clear.
here's current code:
backgroundworker bw = new backgroundworker(); autoresetevent resetevent = new autoresetevent(false); //a windows form progressbar , label loadingoperation loadingform = new loadingoperation(statusmsg); //that form has progressbar that's freezed. want make // not freezed. loadingform.show(); bw.dowork += (sender, e) => { try { if (!e.cancel) { //do } } { resetevent.set(); } }; bw.runworkerasync(); resetevent.waitone(); loadingform.close(); messagebox.show("we done!");
connect backgroundworker
's runworkercompleted
callback close form so:
private void backgroundworker1_runworkercompleted( object sender, runworkercompletedeventargs e) { loadingform.close(); messagebox.show("we done!"); }
you can delete resetevent.waitone();
you'll need make loadingform
field of course.
tell me more
occurs when background operation has completed, has been canceled, or has raised exception
Comments
Post a Comment