We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Loading files without halting the application
Page Index Toggle Pages: 1
Loading files without halting the application (Read 685 times)
Loading files without halting the application
Feb 3rd, 2010, 8:05am
 
I'm working on an application in which I load html pages and do some stuff with them. As it is now I'm using the loadStrings function to get the page so the whole application stops until the page is loaded.

Is there a way to load the page in the background while the rest of the application is still running? So the draw loop would continue looping and once the file is loaded another function fires which processes the page? Actionscript uses on onload event to perform this function.
Re: Loading files without halting the application
Reply #1 - Feb 3rd, 2010, 10:13am
 
There is an asynch image loader, but AFAIK no asynch string loader...
The solution is actually to do the loading in a separate thread. There are several threads about this in the forum.
I just found out that Processing exposes an undocumented thread() function: you call is with a function name, it will call it in a new thread. So you can make a parameter-less function that do the loadStrings and call it this way.
I haven't tested this thread() function yet.
Re: Loading files without halting the application
Reply #2 - Feb 3rd, 2010, 10:23am
 
you'll have to do some multithreading.  what i'd probably do myself is make a class that both stores the string array and handles the threaded loading.  this code is on-the-fly, just to give the ideas, you'll have to debug it yourself, 'k?

Code:

// first, the class:

class ASL extends Thread {
String filename;
String [] strings;
boolean loaded;
ASL(String filename) { this.filename=filename; }
void run() { strings=loadStrings(filename); loaded=true; }
}

// then, use it something like this:

ASL asl;
void setup() {
asl = new ASL("http://processing.org/discourse/yabb2/YaBB.pl");
asl.start();
}
void draw() {
// check status so:
println("has it finished loading yet? " + asl.loaded);
// once loaded use its data so:
if (asl.loaded) {
println(asl.strings.length);
// then some sort of check to not repeat "onload" forever
exit();
}
}

hth
Re: Loading files without halting the application
Reply #3 - Feb 4th, 2010, 1:50am
 
Did you find away to get this done?? Just wondering what option you used i am also looking to do the same thing.

let me know

thanks
Page Index Toggle Pages: 1