c# - How to run an animated SplashScreen at the same time as a Bootstrapper on STA threads -
background:
i working on wpf application uses mef framework automatically link necessary libraries , components app. people adding own components, loading takes anywhere 0-2 seconds 6-7 seconds on slower machines. this, think nice splash screen let user know application loading necessary. ideally splash screen display animated progress bar status text field describing components being loaded.
problem:
i designed splash screen window called right @ start of application (onstartup
), followed loading of mef bootstrapper. problem of course, window not animate because on same thread mef bootstrapper loading. tried putting bootstrapper on separate thread complains not sta thread. on sta thread still didn't , threw errors when trying load main app window. i can't put splash screen window on separate thread because don't have access it, , has sta thread because ui component. (realized untrue, can talk thread)
update
i found solution kept splash screen window in separate sta thread. thank replied pointing me in right direction:
protected override void onstartup(startupeventargs e) { base.onstartup(e); dispatcher threaddispacher = null; thread thread = new thread((threadstart)delegate { threaddispacher = dispatcher.currentdispatcher; synchronizationcontext.setsynchronizationcontext(new dispatchersynchronizationcontext(threaddispacher)); loadingwindow = new loadingwindow(); loadingwindow.closed += (s, ev) => threaddispacher.begininvokeshutdown(dispatcherpriority.background); loadingwindow.show(); system.windows.threading.dispatcher.run(); }); thread.setapartmentstate(apartmentstate.sta); thread.isbackground = true; thread.start(); var bootstrapper = new bootstrapper(); bootstrapper.run(); if (threaddispacher != null) { threaddispacher.begininvoke(new action(delegate { loadingwindow.close(); })); } }
you on right way moving bootstrapper thread of own. should do, make sure portions require being executed on ui thread invoked on ui thread.
so when mef bootstrapper done, can invoke hiding of splash window , opening of main window on main thread, mef's thread.
a way message across main thread dispatcher
Comments
Post a Comment