Saturday, 15 February 2014

c# - Wait until the Splashscreen is visible without blocking the UI thread -



c# - Wait until the Splashscreen is visible without blocking the UI thread -

following situation: got splashscreen has fade in on 2 seconds (from transparent opaque). while window nicely fades in, programme should wait until visible (the storyboard finished) , should go on doing it's stuff. while window fading in, user should see animations of command (so blocking ui thread not option, of course). , animations mean loading circle spins happily.

after found nice variant of how fade in , out window wpf's storyboards, tried accomplish using eventwaithandle. intialization routine runs asynchronous able utilize it. blocked worker thread , stopped application doing initialization stuff before splashscreen visible. somehow has been broken after while , doesn't seem best solution.

here's how i'm doing it, currently:

public async task appear(double time) { core.ide.getguicore().getuidispatcher().invoke(() => { this.opacity = 0; this.show(); _fadeinstoryboard = new storyboard(); _fadeinstoryboard.completed += this.fadeinanimation; doubleanimation fadeinanimation = new doubleanimation(0.0, 1.0, new duration(timespan.fromseconds(time))); storyboard.settarget(fadeinanimation, this); storyboard.settargetproperty(fadeinanimation, new propertypath(opacityproperty)); _fadeinstoryboard.children.add(fadeinanimation); }); _currenthandle = new eventwaithandle(false, eventresetmode.autoreset); core.ide.getguicore() .getuidispatcher() .invokeasync(this._fadeinstoryboard.begin, dispatcherpriority.render); _currenthandle.waitone(); } /// <summary> /// hides splashscreen fade-out animation. /// </summary> /// <param name="time">the fade-out time in seconds.</param> public void disappear(double time) { core.ide.getguicore().getuidispatcher().invoke(() => { _fadeoutstoryboard = new storyboard(); _fadeoutstoryboard.completed += this.fadeoutanimation; doubleanimation fadeoutanimation = new doubleanimation(1.0, 0.0, new duration(timespan.fromseconds(time))); storyboard.settarget(fadeoutanimation, this); storyboard.settargetproperty(fadeoutanimation, new propertypath(opacityproperty)); _fadeoutstoryboard.children.add(fadeoutanimation); }); core.ide.getguicore() .getuidispatcher() .begininvoke(new action(_fadeoutstoryboard.begin), dispatcherpriority.render, null); } private void fadeinanimation(object sender, eventargs e) { _currenthandle.set(); } private void fadeoutanimation(object sender, eventargs e) { this.hide(); }

is right approach? improve solutions? ideas why broken? way, broken mean application continues doing initialization stuff whilst window fading in, ends in animation runs until it's @ 30% visibility , fading out because main window showed up.

thanks in advance

if declare storyboard in xaml, simplify things you. seek using trigger in splash screen control:

<grid> <grid.triggers> <eventtrigger routedevent="loaded"> <beginstoryboard> <storyboard duration="0:0:5" completed="storyboard_completed"> <doubleanimation from="0.0" to="1.0" storyboard.targetproperty="opacity" /> </storyboard> </beginstoryboard> </eventtrigger> </grid.triggers> <!--put ui elements here--> </grid>

you can load rest of app storyboard_completed handler, or if in separate window, can raise event or delegate there, should handle in mainwindow.

c# wpf multithreading storyboard splash-screen

No comments:

Post a Comment