16 Replies Latest reply on Jul 11, 2007 10:31 AM by bdeen

    how to run background timeconsuming task in flex?

    Nick_Evgeniev
      Hi,

      ActionScript programming model is singlethreaded, but it has asynchronous IO calls. This mean that AVM obviously has threads to implement asyncIO.

      I need to perform some time consuming computations within actionscript, but I can't spawn new thread in AS. Hence the questions:

      1. can I somehow implement local service which will receive requests and post responses (events) to consume in GUI (event dispatch thread)? I mean reuse asynchronous IO implementation.

      2. back in windows 3.xx days (single threaded os) there was "nested event loop" approach so within computation loop I can call something like dispatchMessages() to prevent UI from lockup. Is it possible with flex?

        • 1. Re: how to run background timeconsuming task in flex?
          peterent Level 2
          You can't create threads in Flex with ActionScript 3. The best you can do is run part of your code via chained callLater methods.

          When you receive a request, push it onto a queue (Array) and either via a Timer event or callLater, pop off the next item from the queue and process it. If that 'processing' is it self time-consuming, try to run part of it and bookmark it, then use callLater again.
          • 2. Re: how to run background timeconsuming task in flex?
            Nick_Evgeniev Level 1
            quote:

            Originally posted by: peterent
            You can't create threads in Flex with ActionScript 3. The best you can do is run part of your code via chained callLater methods.

            When you receive a request, push it onto a queue (Array) and either via a Timer event or callLater, pop off the next item from the queue and process it. If that 'processing' is it self time-consuming, try to run part of it and bookmark it, then use callLater again.


            Isn't it a bit inFLEXible? :) I'm not asking about implementing thread support in activescript as it's a huge change to avm & language. But either first approach when I implement component that doesn't interact with gui but just posts responses on requests, or the second one when I call hook to dispatch GUI events would be nice to see implemented. That would be
            real step beyond ajax :)
            • 3. Re: how to run background timeconsuming task in flex?
              ProphetJOracle
              This either is on topic or completly off, still trying to figure out, but when I load my flex app "inside vb.net" app I call vb.net to retrieve user info and populate Flex. When I do so, it halts the application even, didnt do that pre-release, they JUST made it that way and is quite irritating. I would select a user and it will go to vb.net and pull that users information and populate the application with all their settings, but doing so *snaps* all the animation sequences so that it basically defeats the entire purpose of having animation sequences completly.

              I have been having the roughest time trying to figure out this multi-threaded issue and in DIRE need of a solution, a 'good' solution. Sure I can hack my way through and get something working, but that usually ends up creating more bugs and too many unknowns.

              Ill try what is suggested, but you would think that this was bound to happen right? I mean, multi-threaded models are quite common right?
              • 4. Re: how to run background timeconsuming task in flex?
                ntsiii Level 3
                I just learned of the property "suspendBackgroundProcessing" which is on mx:Effect. It is false by default, but setting it true causes the effect to finish before any other processing occurs, including user interaction and data loading.

                Perhaps it will help.

                Tracy
                • 5. Re: how to run background timeconsuming task in flex?
                  ntsiii Level 3
                  Nick, suppose you created a swf that had no or minimal UI, that did your processing.

                  You could use localConnection to communicate between the ui swf and the processing swf.

                  Just brainstorming.

                  Tracy
                  • 6. Re: how to run background timeconsuming task in flex?
                    Nick_Evgeniev Level 1
                    quote:

                    Originally posted by: ntsiii
                    Nick, suppose you created a swf that had no or minimal UI, that did your processing.

                    You could use localConnection to communicate between the ui swf and the processing swf.



                    The idea to use LocalConnection is brilliant! I've skipped reading about this class initially because it was positioned (mostly) as a way to comunicate with code written in older AS versions.

                    Quotation from "programming actionscript 3.0" - if embeded code written in previous version of AS the only way to communicate with it is to use LocalConnection. It can be read as -- your old code will be executed by previous AVM version in
                    separate THREAD. Yikes!

                    The only question I still have unclear is: If embeded code is written in AS3 but I still use LocalConnection to communicate with it will AVM spawn a new thread for my .swf or not? Does anybody know where to get docs on AVM internals?

                    For code deployed as separate swfs within same .html page everything is clear -- it's been run by separate AVM instances in separate threads.



                    • 7. Re: how to run background timeconsuming task in flex?
                      inlineblue Level 1
                      I don't think it'll spawn a new thread for an AS3 SWF. The reason it starts a new thread for AS2 is because there're actually two VMs in the player, one for AS2 and one for AS3.

                      But, you know, using an AS2 SWF to run calculations in the background is an intriguing idea. Wish I had more time to play with ideas like that.
                      • 8. Re: how to run background timeconsuming task in flex?
                        ntsiii Level 3
                        Honestly,.I don't understand the Flash Player well enough to be sure how this would work.

                        However, intuitively speaking, if you have two different swfs in a browser, I would expect them to each have their own, independent main processing thread. I mean, they are fully independent instances of the player, aren't they?

                        I'm sure we could reasearch this if we wanted.

                        Tracy
                        • 9. Re: how to run background timeconsuming task in flex?
                          bdeen Level 1
                          I tried this trick, but I got the same pause as a time consuming event occurred. Has anyone confirmed that ntsii's idea works?
                          • 10. Re: how to run background timeconsuming task in flex?
                            bdeen Level 1
                            Also is background processing something Flash will ever offer? I'm trying to calculate Levenshtein Distances and it isn't difficult just hard to create the matrix needed to map from one word to the other word, so it takes some cpu time, and I want to do this calculation in the background as someone is contsantly typing. plus i don't want the main UI to be unusable while I wait for a simple but intensive calculation to finish.
                            • 11. Re: how to run background timeconsuming task in flex?
                              ntsiii Level 3
                              Some folks would probably say you should do this on the server. That is not an answer, I know..
                              Tracy
                              • 12. Re: how to run background timeconsuming task in flex?
                                bdeen Level 1
                                I agree however I have request for a client only implementation. So I think my only hope is to create an Actionscript 2 version for the distance and try that with a localconnection
                                • 13. Re: how to run background timeconsuming task in flex?
                                  ntsiii Level 3
                                  Please let us know how this works. I have a situation where it would be useful.
                                  Tracy
                                  • 14. Re: how to run background timeconsuming task in flex?
                                    bdeen Level 1
                                    ntsiii

                                    I'm afraid i didn't try this. I background prpcess by doing a little at a time via call later saw a big performance increase in ui responsiveness
                                    • 15. Re: how to run background timeconsuming task in flex?
                                      levancho Level 3
                                      why cant you just let server side do the calculation and resource intensive tasks, servers are mostly more powerfully fast and efficient,than client machines, and with async requests user would not even see a blink and there will not be any wait time or at least minimal , I don't think its justifiable to do Levenshtein Distances algorithm calculations or things of such nature, on client side thats too heavy I think..
                                      either way you do it in other swf file or not it still uses clients cpu overall, so at the end client suffers anyways.
                                      (thats just what I think )
                                      • 16. Re: how to run background timeconsuming task in flex?
                                        bdeen Level 1
                                        I completely understand that which is why my first version of my product used a webservice instead of client operation. I've been requested by a client to see if its feasible to do clientside only implementation. The distant algorithm processes pretty quickly there's just a lot of checks to do for a bunch or different words. I can see a use of this if you allow users to disconect from the server or if you have worries of bandwidth (which probably isn't the case here). If the end user which is a technical developer wants a client side only I'm more then happy to help them out, and I'm sure there will be others with various needs for the same implementation. And if I use a calllater and minimize my calculations then I'm taking cycles that aren't being used by the app. But I agree that a server version is a better implementation but with everything theres give and
                                        take you give up something to gain something. And not a lot of developers want to be questioned about the validity of their technical decision.