1 Reply Latest reply on Feb 4, 2010 1:34 PM by msakrejda

    pausing execution within a function


      Here is what I'm trying to do:


      1. Use the ImageSnapshot and FileReference classes to save a chart as an image. Works.



      2. Set a specific style of the chart to be rendered for the image snapshot. For example, I need a pie chart that defaults to rollover labels to change to call-outs for the snapshot, and then change back. Unfortunately, the snapshot does not wait for the style to finish updating.


      After searching around for some kind of wait-for-thread function, I thought I finally had it when I found callLater. This might have worked if I didn't get the dreaded...


      Error #2176: Certain actions, such as those that display a pop-up window, may only be invoked upon user interaction, for example by a mouse click or button press.


      Apparently a security restriction prevents FileReference from being accessed anywhere outside of the onClick function. Here is a discussion on that:



      So now what I am looking for is some way to pause the execution inside of a function, to let the style finish updating before taking the snapshot. Here is a sample of the code I am trying to use.


      private function saveAsImage():void {
           var lp:String = pieSeries.getStyle("labelPosition"); //Remember old style
           pieSeries.setStyle("labelPosition", "callout");
           //NEED TO WAIT HERE
           var image:ImageSnapshot = ImageSnapshot.captureImage(pieChart, 300, new PNGEncoder(), true);
           var file:FileReference = new FileReference();
           file.save(image.data, "chart.png");
           pieSeries.setStyle("labelPosition", lp); //Revert to old style


      Other ideas for solutions are welcome. Thanks.

        • 1. Re: pausing execution within a function
          msakrejda Level 4

          You can't block control flow like that in Flash Player (and anyway, even if you could, it wouldn't do much good since there's only a single thread, and you would be blocking it). An alternative approach might be to build two pie charts: one to show and one to save. Granted, that's pretty ugly, but it should work.