Posted by frankieb2007 in SWF Studio V3 on Mar 25 2008, 02:50 pm

is there any difference between the following methods ?

   ssDefaults.synchronousCommands = true;
   
   // method #1
   var return_obj = ssCore.FileSys.dirExists({path:WhateverPath});
   if (return_obj.result == 'TRUE'){
       // dir exists
   }else{
       // dir does not exist
   }

   // method #2
   ssCore.FileSys.dirExists({path:WhateverPath},{callback:"MyCallback"});
   function MyCallback(return_obj,callback_obj,error_obj){   
      if (return_obj.result == 'TRUE'){
          // dir exists
      }else{
          // dir does not exist
      }   
   }


Posted by @st@l@vist@ in SWF Studio V3 on Mar 25 2008, 03:20 pm

It is simply :)

ssCore.init();
ssDefaults.synchronousCommands = true;
var path = "startdir://FolderName";

ssDebug.trace("--- Synchronous ---");

ssDebug.trace("1");
var r = ssCore.FileSys.dirExists({path:path});
ssDebug.trace(r.result);
ssDebug.trace("2");

ssDebug.trace("--- Asynchronous ---");

ssDebug.trace("1");
ssCore.FileSys.dirExists({path:path},{callback:MyCallback});
function MyCallback(r,c,e)
{   
   ssDebug.trace(r.result);   
}
ssDebug.trace("2");


Posted by mbd in SWF Studio V3 on Mar 25 2008, 03:43 pm

The first is synchronous and the second is asynchronous. The data returned by the first command will be available to the code in the next line. The data returned by the second command will only be available to the callback function after X period of time - at least one frame.

Synchronous commands stop any further execution of ActionScript until data is returned. Asynchronous commands are sent to the Flash Player, which queues them and passes them to the SWF Studio player once all ActionScript has completed for that frame. Responses are then only available for the next frame or afterwards.

You would use Asynchronous commands to allow Flash to continue doing its thing while the SWF Studio player executes code that might take some time, like accessing a web server through the HTTP or FTP plugins.

If a synchronous command takes longer than 15 seconds, the Flash Player will display a dialog indicating that a script is running slowly. During the time when Flash is waiting for data the application is non-responsive. If you suspect that a command may take some time or stop user interaction at an inappropriate time use the asynchronous method.


Posted by frankieb2007 in SWF Studio V3 on Mar 25 2008, 05:43 pm

i'm still coding in AS2, and i'll definitely have some
functions which will take more than 15 seconds to complete.
the functions just involve creating a file system, which
include some files which may have to be downloaded before
continuing.

by keeping all my code on one frame, i could add at the
very end of the frame a check for "all" files and folders,
and if any do not exist, then repeat the same frame... and
keep doing so, until files and folders are found. does this
sound like a standard method or do you suggest something else?

i assume there's no need for me to monitor the download if
flash will timeout within 15 seconds anyway. i'd prefer
doing everything on a single frame if possible.


Posted by mbd in SWF Studio V3 on Mar 26 2008, 10:04 am

You may have misunderstood what I meant when I referred to the 15 second timeout and what I meant about async commands and frames. Let me see if I can help clear up the issues.

When I mentioned a 15 second timeout, I was referring to how the Flash Player handles a situation where code runs continuously in the span of 1 frame. I don't mean that you have to spread your code out over multiple frames.

If you load a file externally, like an XML document or an SWF or image, it doesn't happen within 1 frames amount of time.

For instance, here I'm loading an XML file:

var my_xml = new XML();
my_xml.ignoreWhite = true;
my_xml.load('file.xml');
my_xml.onLoad = function (success)
{
   if (success) trace('XML loaded');
   else trace('XML not loaded');
}
trace('after the call to load the XML');

In the above code the text "after the call to load the XML" will be displayed before the text in the onLoad function. Any other code that runs in that code block will run before the onLoad function is called. The onLoad function will be called in the next frame at the earliest. Notice I said "in the next frame" but the code is still written in the same frame as the rest of the code.

A script within a code block has 15 seconds to run before the Flash Player will display the "script running slowly" dialog.

When you say that your code will take longer than 15 seconds to complete, do you mean that you are expecting a single code block to run for that much time? You can setup a process to take longer than 15 seconds - the Flash Player will allow SWFs to run longer than that! You just can't have code that will block other code from running for longer than 15 seconds.

For instance, this code will block all other code from running for longer than 15 seconds:

var timer = getTimer();
while(getTimer() - timer < 20000)
{
}
trace('after the while loop');

The only way you will see the trace above is if you do not abort the script when the Flash Player displays the dialog (or if you increase the script limits, but that's for another discussion).

So, you don't need to use multiple frames for your code. You can if you wish, but it's not a requirement.

When you want to think of async vs. sync, think of XML.load and XML.onLoad vs. a typical function call.

An async call is sent to the SWF Studio player (like XML.load sent to the Flash Player) and your callback (like XML.onLoad) is called at some point in the future. You don't need to know when. You just know that it won't be called immediately.

A sync call is the same as doing this in Flash:

function test()
{
   return 'blah';
}
var b = test();
trace(b);

Tracing the variable b will display "blah" since the call is immediate. Now, instead of just returning a simple string "blah", what if the function test actually did some other functionality that took a really long time? The trace would still display "blah", but if test took longer than 15 seconds, the "script running slowly" dialog would appear.

Does that help?