DFL background tasks

thedeemon via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon Jun 8 00:08:04 PDT 2015


On Sunday, 7 June 2015 at 13:34:59 UTC, Scroph wrote:
> I tried your code and it did work, however it still froze the 
> UI while it was retrieving the data.

Sure, because now you do your long operation in the UI thread. 
What happens in the delegate passed to invoke() happens in the 
main UI thread.

You've got two steps: first some long operation producing some 
data, then updating the listview with this data. You need to do 
the long processing part in a worker thread and updating in the 
UI thread. This is how it's done:

in the button callback (which runs in UI thread) you spawn() a 
thread with some worker function. This worker function does your 
long operation, and it happens in separate thread so the UI keeps 
responding. After you finished this long operation you call 
listview.invoke() with a delegate that just updates the listview, 
it doesn't do any long operations. This update will happen in the 
UI thread.

Something like:

void loadDetailsCallback(Control sender, EventArgs ea)
{
   ...
   spawn(&loadDetails, qualityBox);  // quickly launch a thread
   // and go on responding to UI events
}

void loadDetails(ListView qualityBox) {
   //do the work, this happens in worker thread
   vid = ...

   qualityBox.invoke( { // this part happens in UI thread, quickly
     qualityBox.beginUpdate(); // this belongs out of loop
     foreach(q; vid.qualities) {
       qualityBox.addRow(q.itag.to!string, q.type, q.quality, 
q.link);			
     }
     qualityBox.endUpdate();
   });
}



More information about the Digitalmars-d-learn mailing list