Is it possible to call a function asynchronously in Flex? I want to parse a file at regular intervals without blocking the rest of the application, what is the recommended approach for this?
Actionscript doesn't support multithreading, which I think is what you are really asking about here.
While the functionality isn't inherent in actionscript (or Flex) you could set up a mock system using events and timers.
I'm a little unclear on your exact question, so I'll give two answers:
1) You want to process a file every few seconds to act on any changes.
In this case all you need to do is set up a timer to check the file periodically:
var fileTimer:Timer = new Timer(5000);
fileTimer.addEventListener(TimerEvent.TIMER, checkFile);
...
private function checkFile(event:TimerEvent):void {
// read the file and do whatever you need to do.
}
2) You want to parse a very large file but don't want the application to hang whilst doing it.
If you want to process the file in the background, while keeping the main application responsive then I would probably create a function that would parse several lines of the file and then send an event and return. Listen for the event and start a timer that would wait a few milliseconds before calling the function again to parse the next set of lines.
This would break up the parsing of a large file with enough down time to keep the rest of your app running smoothly. You'd have to play with the timer interval and number of lines to parse at once to strike a good balance of responsiveness and the time needed to parse the file.
Hope that makes sense!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With