c# - Two background tasks in a row and loading gif (in)visibility -


i've been banging head on desk while problem.

so, setup: have form doing "heavy" operations (data loading, etc...). have thread created on fly perform these (in, let's say, "doworkinbackground" method), , form displays small animated gif meanwhile. detail that, @ start of method, gif made visible, , invisible @ end, both being done asynchronously (to thread safe). problem being @ point of program, have 2 calls doworkinbackground in row, , second 1 seems called before gif made invisible first... therefore, gif remains invisible duration of second call.

-general methods

// perform thread-safe modification on control protected void addcontrolwork(control c, action a) {     if (c.invokerequired)         c.begininvoke(new methodinvoker(a));     else         a(); }  // perform background task, displaying loading gif , disabling other controls protected void doworkinbackground(methodinvoker ) {     addcontrolwork(loadingpicture, delegate() { loadingpicture.visible = true; });     addcontrolwork(mainpanel, delegate() { mainpanel.enabled = false; });     f.begininvoke(new asynccallback(endworkinbackground), null); }  // @ end of background task, hides loading gid, enables other controls protected void endworkinbackground(iasyncresult result) {     if (this.disposing)         return;     addcontrolwork(loadingpicture, delegate() { loadingpicture.visible = false; });     addcontrolwork(mainpanel, delegate() { mainpanel.enabled = true; }); } 

-usage:

private void alistbox_selectedindexchanged(object sender, eventargs e) {     doworkinbackground((methodinvoker)(() =>     {         object[] source = heavyoperation();;         addcontrolwork(acombobox, delegate()         {             // changing datasource fires acombobox.selectedindexchanged             // including second call doworkinbackground             acombobox.datasource = source;         });     })); }  private void acombobox_selectedindexchanged(object s, eventargs e) {      doworkinbackground((methodinvoker)(() =>     {         // uninteresting stuff         object[] range = aheavyloading();         object[] source = anotherheavyloading();         addcontrolwork(unelistbox, delegate() { alistbox.items.addrange(range); });         addcontrolwork(unecombobox, delegate() { acombobox.datasource = source; }); }));  } 

in exemple above, can see when alistbox_selectedindexchanged called, acombobox_selectedindexchanged called right after. there problem, gif remains invisible.

i don't have access await/async. if have solution, that'd appreciated, thanks.

note: put problem here sake of curiosity. "solved" problem preventing firing of acombobox_selectedindexchanged in alistbox_selectedindexchanged.


Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -