c# - instantiate global variables in parallel way within WPF application -
in wpf application , instantiation of viewmodel classes takes lot of time, think, solve issue, create static objects when launch application :
protected override void onstartup(startupeventargs e) { viewmodellocator locator = (viewmodellocator)app.current.resources["locator"]; loginwindowviewmodel.objficheviewmodel = locator.ficheviewmodel; loginwindowviewmodel.objformationsviewmodel = locator.formationsviewmodel; loginwindowviewmodel.objfacturationviewmodel = locator.facturationviewmodel; loginwindowviewmodel.objgestiondpcviewmodel = locator.gestiondpcviewmodel; loginwindowviewmodel.objgestiongdpviewmodel = locator.gestiongdpviewmodel; }
so need know :
- is solution?
- how improve make instantiation work in parallel way avoid ui pause( instantiation part takes 5 seconds!!)
you can make use of async
, await
set viewmodels.
consider this:
create property called issettingup in loginwindowviewmodel
, so:
private bool _issettingup; public bool issettingup { { return _issettingup; } set { _issettingup = value; //on property changed stuff onpropertychanged(); } }
then create async
method responsible creating viewmodels.
public async void setup() { this.issettingup = true; await setupviewmodels(); //other initialization stuff here if needed this.issettingup = false; }
and setupviewmodels method this:
private async task setupviewmodels() { await task.factory.startnew(() => { viewmodellocator locator = (viewmodellocator)app.current.resources["locator"]; loginwindowviewmodel.objficheviewmodel = locator.ficheviewmodel; loginwindowviewmodel.objformationsviewmodel = locator.formationsviewmodel; loginwindowviewmodel.objfacturationviewmodel = locator.facturationviewmodel; loginwindowviewmodel.objgestiondpcviewmodel = locator.gestiondpcviewmodel; loginwindowviewmodel.objgestiongdpviewmodel = locator.gestiongdpviewmodel; }); }
to make use of issettingup property, consider creating control visible if issettingup true. perhaps loading icon, or screen overlay. ensure ui remain responsive while viewmodels being created.
Comments
Post a Comment