Implement a threading to prevent UI block on a bug in an async function
- by Marcx
I think I ran up againt a bug in an async function...
Precisely the getDirectoryListingAsync() of the File class...
This method is supposted to return an object containing the lists of files in a specified folder.
I found that calling this method on a direcory with a lot of files (in my tests more than 20k files), after few seconds there is a block on the UI until the process is completed...
I think that this method is separated in two main block:
1) get the list of files
2) create the array with the details of the files
The point 1 seems to be async (for a few second the ui is responsive), then when the process pass from point 1 to point 2 the block of the UI occurs until the complete event is dispathed...
Here's some (simple) code:
private function checkFiles(dir:File):void
{
if (dir.exists)
{
dir.addEventListener( FileListEvent.DIRECTORY_LISTING, listaImmaginiLocale);
dir.getDirectoryListingAsync();
// after this point, for the firsts seconds the UI respond well (point 1),
// few seconds later (point 2) the UI is frozen
}
}
private function listaImmaginiLocale( event:FileListEvent ):void
{
// from this point on the UI is responsive again...
}
Actually in my projects there are some function that perform an heavy cpu usage and to prevent the UI block I implemented a simple function that after some iteration will wait giving time to UI to be refreshed.
private var maxIteration:int = 150000;
private function sampleFunct(offset:int = 0) :void
{
if (offset < maxIteration)
{
// do something
// call the recursive function using a timeout..
// if the offset in multiple by 1000 the function will wait 15 millisec,
// otherwise it will be called immediately
// 1000 is a random number for the pourpose of this example, but I usually change the
// value based on how much heavy is the function itself...
setTimeout(function():void{aaa(++offset);}, (offset%1000?15:0));
}
}
Using this method I got a good responsive UI without afflicting performance...
I'd like to implement it into the getDirectoryListingAsync method but I don't know if
it's possibile
how can I do it
where is the file to edit or extend..
Any suggestion???