Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asynchronous function call in Flex

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?

like image 374
Abdullah Jibaly Avatar asked Feb 19 '09 02:02

Abdullah Jibaly


1 Answers

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!

like image 195
Matt Guest Avatar answered Oct 10 '22 15:10

Matt Guest