PDA

View Full Version : try to load stuff until it succeeds



no_support
November 15th, 2008, 12:38 PM
given:
Java is buggy when you write with ImageIO.write. (hint: takes more than 20 seconds to finish up writing to a .png file).
They are laying off 6000 people at sun (or so i heard), some of which are java people so this bug won't get fixed any time soon.

I want:
a work around in flash. Basically I would like to check to see if the file created by Java that flash is waiting on is ready and if it's not ready keep trying to load it until it is ready at which point the "try to load" will succeed. Is there any way to do this? I see that calling the load function in caught/listened errors won't cause a loop back to the catch block or event listener again if the first error is caught/listened so that isn't a working strategy. What i need is some loop that checks if the file is available yet that runs until it is available.

babeler
November 15th, 2008, 01:56 PM
Hi,

I'm not exactly sure of the last lines you wrote, but this should work:


function doLoad(file:String):void {
// create here Loader for load and add listeners
}
function errorListener(e:IOErrorEvent):void {
// here, if an errore is found, you might call again doLoad("myfile.png");
// though, use a timer to wait at least a few milliseconds before loading again
}

no_support
November 15th, 2008, 02:57 PM
That's exactly what i was trying to avoid. for some reason that strategy doesn't work. I tried to step through using the debugger and the function is called only once for some reason. I have also tried setInterval. There really should be a way for actionscript to wait until a resource is ready before actually trying to access it. That's just the way a client server model works.

babeler
November 15th, 2008, 05:04 PM
Strange. I'm actually loading an XML in a project, and if it doesn't load because of some error, I reload it again, and it works.
If you want, you might post your code for loading/reloading, thus we might check it for eventual bugs.

Oh, and sadly no: there is no default "wait for readiness" feature in AS, at least as far as I know.

Sage_of_Fire
November 15th, 2008, 05:46 PM
Okay, assuming that your java script just malfunctions half the time, and you want to keep trying until you get the load, I'd suggest using the timer class. You just plug in an interval, set a timer listener, and keep calling the load function till it works. Here's some psuedocode; you'd have to consult the AS3 reference on Adobe.com to get the actual implementation.


var timer:Timer = new Timer();
timer.(interval) = 1000;
timer.addEventListener(Timer.RING, tryload);
timer.start();

function tryload(evt:TimerEvent):void {
try {
loader.load(new URLRequest('url.txt'));
loader.addEventListener(Loader.INIT, tryINIT);
} catch (e:Error);
}

function tryINIT(evt:Event):void {
try {
var data = loader.content;
// do something with data
timer.removeEventListener(Timer.RING, tryload);
timer.stop();
timer = null;
// go on with rest of script
} catch (e);
}

Yeah, that code's even rougher than I anticipated. Still, I think that's what your looking for. This code should attempt to load the data, attempt to read the loaded data once it's done loading, and keep doing that until it gets something. Maybe this will solve your problem.