c# - WPF modal progress window -


i apologize if question has been answered tons of times, can't seem find answer works me. create modal window shows various progress messages while application performs long running tasks. these tasks run on separate thread , able update text on progress window @ different stages of process. cross-thread communication working nicely. problem can't window on top of other application windows (not every application on computer), stay on top, prevent interaction parent window, , still allow work continue.

here's i've tried far:

first, splash window custom class extends window class , has methods update message box. create new instance of splash class on , show/hide needed.

in simplest of cases, instantiate window , call .show() on it:

//from inside secondary thread this._splash.dispatcher.invoke(new action(() => this._splash.show());  //do things //update splash text //do more things  //close splash when done this._splash.dispatcher.invoke(new action(() => this._splash.hide()); 

this correctly displays window , continues running code handle initialization tasks, allows me click on parent window , bring front.

next tried disabling main window , re-enabling later:

application.current.dispatcher.invoke(new action(() => this.mainwindow.isenabled = false));  //show splash, things, etc  application.current.dispatcher.invoke(new action(() => this.mainwindow.isenabled = true)); 

this disables elements in window, can still click main window , bring in front of splash screen, not want.

next tried using topmost property on splash window. keeps in front of everything, , in conjunction setting main window isenabled property prevent interaction, makes splash screen appear in front of everything, including other applications. don't want either. want topmost window within application.

then found posts using .showdialog() instead of .show(). tried this, , correctly showed dialog , did not allow me click on parent window, calling .showdialog() makes program hang waiting close dialog before continue running code. obviously, not want either. suppose call showdialog() on different thread that thread hang thread doing work not...is recommended method?

i have considered possibility of not using window @ , instead putting full-sized window element in front of else on page. work except have other windows open , i'd able use splash screen when open too. if used window element have re-create on every window , wouldn't able use handy updatesplashtext method in custom splash class.

so brings me question. right way handle this?

thanks time , sorry long question details important :)

you correct showdialog gives of ui behavior want.

it have problem call block execution though. how possibly run code after show form, define should before it's shown? that's problem.

you of work within splash class, that's rather poor practice due tight coupling.

what can leverage loaded event of window define code should run after window shown, defined before show it.

public static void doworkwithmodal(action<iprogress<string>> work) {     splashwindow splash = new splashwindow();      splash.loaded += (_, args) =>     {         backgroundworker worker = new backgroundworker();          progress<string> progress = new progress<string>(             data => splash.text = data);          worker.dowork += (s, workerargs) => work(progress);          worker.runworkercompleted +=             (s, workerargs) => splash.close();          worker.runworkerasync();     };      splash.showdialog(); } 

note method designed encapsulate boilerplate code here, can pass in worker method accepts progress indicator , work in background thread while showing generic splash screen has progress indicated worker.

this called this:

public void foo() {     doworkwithmodal(progress =>     {         thread.sleep(5000);//placeholder real work;         progress.report("finished first task");          thread.sleep(5000);//placeholder real work;         progress.report("finished second task");          thread.sleep(5000);//placeholder real work;         progress.report("finished third task");     }); } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -