all groups > flash actionscript > january 2005 >
You're in the

flash actionscript

group:

PrintJob causes CPU hang



PrintJob causes CPU hang funkykeys
1/2/2005 7:02:06 PM
flash actionscript: Hi, I'm getting CPU hangs w/ that dreaded 'A script is causing the Flash
player to run slowly...' error when trying to print. In a nutshell, I'm
allowing the user to print a history of stock transactions from a stock market
game. Here's a sample string, which as you can see isn't extremely long (see
quote below). To print, I'm using PrintJob() and dynamically filling a
TextField name 'text_txt' with the string below. Below the sampel string is my
code. Any ideas on what would cause the problem? As a note, I've tested this
by creating/populating new text fields on the fly, and the loop works great.
Once PrintJob is in the picture, all hell breaks loose. <b><span
class='blue'>January 1, 2005 at 10:01 PM</span></b><br><b>Purchased 10 Pixar
shares at $85.61/share.</b><br>Reasons for this purchase: I just love to buy
Pixar shares. My dad is Steve Jobs.<br><p align='center'><img src='blueDot.jpg'
height='1' width='440' hspace='0' vspace='7'></p><b>Purchased 15 Lionsgate
shares at $10.62/share.</b><br>Reasons for this purchase: Lions Gate is a good
company, but they make bad coffee.<br><p align='center'><img src='blueDot.jpg'
height='1' width='440' hspace='0' vspace='7'></p><b>Purchased 100 Dreamworks
shares at $37.51/share.</b><br>Reasons for this purchase: I'm not quite sure.
DreamWorks' line of cars really sucks.<br><br><br><b><span class='blue'>January
1, 2005 at 10:02 PM</span></b><br><b>Purchased 5 Pixar shares at
$85.61/share.</b><br>Reasons for this purchase: Did I? I forgot what
happened.<br><p align='center'><img src='blueDot.jpg' height='1' width='440'
hspace='0' vspace='7'></p><b>Purchased 6 Lionsgate shares at
$10.62/share.</b><br>Reasons for this purchase: I forgot that I was supposed
to buy my mom some shares.<br><p align='center'><img src='blueDot.jpg'
height='1' width='440' hspace='0' vspace='7'></p><b>Sold 20 Dreamworks shares
at $37.51/share.</b><br>Reasons for this purchase: Like many Wall Street
yuppies, I need the money for drugs. I've been buying WAY too many stocks these
days and my mind is fried.<br><br><br>

//init misc printing vars
var NUM_Y_OFFSET:Number = 10;
var pmc:MovieClip = _root.printInst_mc;
var smc:MovieClip = _root.stocks_mc;
var numWhichPage:Number = 0;
var numTextMaxHeight:Number = pmc.text_txt._height;
var numTextHeight:Number = 20;
var numDepth:Number = 0;
var numFields:Number = 20;
var text_array:Array = new Array();
pmc._xscale = 72;
pmc._yscale = 72;
var strNoPrintYet:String = "Printing is not yet available."
import mx.controls.Alert;

//set print text properties
pmc.text_txt.styleSheet = _root.myCSS;

//init print button functions
print_btn.onRelease = function() {
//start a new print job
var pj:PrintJob = new PrintJob();
var myResult = pj.start();

//if "OK," start print job
if (myResult) {
//pull global SO data
var global_so:SharedObject = SharedObject.getLocal("global");
var strName:String = global_so.data.strUserName;
var strClass:String = global_so.data.strClassName;
var endDate:Date = new Date(global_so.data.endDate);
var strDate:String = smc.months_array[endDate.getMonth()] + " " +
endDate.getDate() + ", " + endDate.getFullYear();
var strStudentInfo:String = "<span class='blue'><b>Student
Name:</b></span>&nbsp;&nbsp;&nbsp;" + strName + br +
"<span class='blue'><b>Class Name:</b></span>&nbsp;&nbsp;&nbsp;" + strClass
+ br +
"<span class='blue'><b>Game End Date:</b></span>&nbsp;&nbsp;&nbsp;" +
strDate;

//populate text field and array with intro data
pmc.text_txt.htmlText = "<p class='blueHeader'>Student Information</p><p>" +
strStudentInfo +
"</p>" + br2 + br2 + "<p class='blueHeader'>Summary of Previous
Transactions</p><p>";
text_array[numWhichPage] = "<p class='blueHeader'>Student
Information</p><p>" + strStudentInfo +
"</p>" + br2 + br2 + "<p class='blueHeader'>Summary of Previous
Transactions</p><p>";

//init text string to hold dynamic print text
var strDataString:String = text_txt.htmlText;

//loop through string and populate text field and array
for (var i:Number=0; i<strDataString.length; i++) {
//if current text field's height is greater than numtextHeight,
//clear text field and populate new array position
if (pmc.text_txt.textHeight > numTextMaxHeight - numTextHeight &&
strDataString.charAt(i-1) == " ") {
//close current text field and array position
pmc.text_txt.htmlText += "</p>";
text_array[numWhichPage] += "</p>";

//clear text field and add new page header
pmc.text_txt.htmlText = "";
pmc.text_txt.htmlText = "<p class='blueHeader'>Summary of Previous
Transactions&nbsp;&nbsp;&nbsp;(continued)</p><p>";

//increment page number and add new page header to new array position
numWhichPage++;
text_array[numWhichPage] = "<p class='blueHeader'>Summary of Previous
Transactions&nbsp;&nbsp;&nbsp;(continued)</p><p>";
}

//populate current text field
pmc.text_txt.htmlText += strDataString.charAt(i);
text_array[numWhichPage] += strDataString.charAt(i);
}

//now re-populate all fields with array values and add page(s) to print
var numPages:Number = text_array.length;
for (var i:Number=0; i<numPages; i++) {
pmc.text_txt.htmlText = text_array[i];
pmc.page_txt.text = "Page " + (i+1) + " of " + numPages;
pj.addPage("_root.printInst_mc", {xMin:0, xMax:800, yMin:0, yMax:2000},
null, 1);
}

//send all pages to the spooler
pj.send();

//delete print job
delete pj;

//now clear text field
pmc.text_txt.htmlText = "";
}
}
Re: PrintJob causes CPU hang TinMonkey
1/3/2005 2:57:24 AM
from what I have read about the printJob function, I am under the impression
that you want to set all your variables and such before you start a new
printJob. There is a time limit on how much time can pass from the time you
start a job until the time you send it. I would try puting the varible stuff
before the start printJob commands. Your code example is the most I've ever
seen a script call for inbetween the start and send commands. Hope this helps
Re: PrintJob causes CPU hang funkykeys
1/3/2005 4:07:06 AM
Thanks for the reply. I moved the PrintJob after the vars, but I was mistaken:
the hang is happening when looping through string and populating the text field
and array. I populate long text strings dynamically all the time, but maybe
because the loop increment is one per character (which was an example MM gave),
there's apparently too much happening in the loop, causing it to hang. MM has
always failed to understand the concept of printing.
Re: PrintJob causes CPU hang funkykeys
1/3/2005 4:07:39 AM
Thanks for the reply. I moved the PrintJob after the vars, but I was mistaken:
the hang is happening when looping through string and populating the text field
and array. I populate long text strings dynamically all the time, but maybe
because the loop increment is one per character (which was an example MM gave),
there's apparently too much happening in the loop, causing it to hang. MM has
always failed to understand the concept of printing. Pagination should never
have to be left to the programmer -- the OS does it already!
AddThis Social Bookmark Button