application becomes unresponsive during directory listing in flex 3
I have designed an AIR application that displays a list with all txt files located in C:\ directory. I have used following code:
var videoListsArr:ArrayList = new ArrayList();
var folder:File = new File(driveName+":\\");
folder.getDirectoryListingAsync();
folder.addEventListener( FileListEvent.DIRECTORY_LISTING, handleDirectoryListing );
private function handleDirectoryListing( event:FileListEvent ):void
{
for each(var item:File in event.files)
{
var itemExtn:String = (item.extension != null) ? item.extension.toLocaleLowerCase() : null;
if(item.isDirectory)
{
item.getDirectoryListingAsync();
item.addEventListener( FileListEvent.DIRECTORY_LISTING, handleDirectoryListing );
}
else if(!item.isSymb开发者_运维问答olicLink && itemExtn != null)
{
if(itemExtn == "txt")
videoListsArr.addItem(txt);
}
}
}
This function works fine but it is being executed the application is hang and become unresponsive. Please tell me how to resolve this problem that it displays the list of txt file without making application unresponsive ?
from what i can see, you're getting a directory, looking at all the files in there, and if one of them is a directory, you look at all the files etc recursively, which could end up being very heavy depending on the start folder. remove the first if check and tell me if it's still unresponsive
I use a timer to break up the processing. My timer subclasses Timer to add a files:Array and a directories : Array.
The initial handler that processed the top-level directory (in my case, loadDirectoryHandler) loads the timer.files with all the files it cannot processes immediately as well as all the sub-directories,
On each cycle through the TIMER handler, it slices out a chunk of the files Array (e.g. 200 files), processes that, and then starts the timer if there are any left so they get processed on the next TIMER event.
For each directory, it takes the File object and
file.addEventListener( FileListEvent.DIRECTORY_LISTING, loadDirectoryHandler ); file.getDirectoryListingAsync();
Cheers
精彩评论