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?
相关问题
- garbage collection best practices
- How to load flex swf from flash?
- Load script async?
- C# An asynchronous operation cannot be started at
- aio_write on linux with rtkaio is sometimes long
相关文章
- With a Promise, why do browsers return a reject tw
- Asynchronous SHA256 Hashing
- Does aiohttp have ORM?
- Can each Iteration of a for loop/for_each be done
- How does robospice manage activity lifecycle?
- How to call a async function from a synchronized c
- passing multiple arguments to promise resolution w
- Read headers from HttpResponseMessage before Conte
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:
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!
There is also a
setInterval
function which will call a function at regular intervals which could be useful.setTimeout and setInterval are both deprecated.
The Timer class not only allows a delay, but also a repeatCount: how many times it will throw a TIMER event and start counting down again. Presumably, one would call myTimer.stop() inside the event before doing whatever you wanted to do, and myTimer.start() when one was done.
Cheers
Interestingly, just yesterday I described a solution to this problem in response to a different question.
The simplest answer is to use the
callLater
routine - see some documentation here.Another approach is to use the
setTimeout
call, defined in theflash.utils
package. This one lets you call a routine after a specified amount of time has passed. Using this routine, you could set up yourparseFile
function to call itself repeatedly, giving you the regular intervals you were looking for:What you need is a concept called Green threads. There is a green threading lib out there, but I have not used it.
The implementation I did for an import process (1 - 5 minutes) actually tracked how long the executions were taking and allowed for a configurable amount of time per cycle. This allows you to choose the number of frames you dropped (we were simply updating a modal progress bar). I also subclassed it with a version that ran through a ICollectionView with a IViewCursor and fired off the event with each item.
I cannot supply the source code due to it's connection with a commercial product, but the concept is quite simple to implement.