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 › XMLElement inside multithreaded class
Page Index Toggle Pages: 1
XMLElement inside multithreaded class? (Read 407 times)
XMLElement inside multithreaded class?
May 30th, 2008, 5:05pm
 
hi again.

i wonder how to get XMLElement to work inside a class.
i had it inside a function and it worked just fine. but since the xml-thingy takes quite a while to load i wanted to multithread it so i can display a loading screen or something thelike.

unfortunately if i put it inside a class it doesn't work anymore. i think it's because in the previous version i had xmlartisttag = new XMLElement(this, getthisartist); and inside a class the "this" won't work...but xmlartisttag = new XMLElement(getthisartist); will start the application but will then "crash" because it can't parse.


inside multithreaded class:
Quote:


class CL_artisttagloader extends Thread{
XMLElement xmlartisttag;

 public void run() {
     for (int i = 0; i < artistnum; i++)
     {
 
         Curstatus = "Searching Tags for "+personalcharts[i].artist;
         
         String getthisartist = convertencoding(personalcharts[i].artist);
         getthisartist = "http://ws.audioscrobbler.com/1.0/artist/"+getthisartist+"/toptags.xml";
       
         println(getthisartist);
         try {
           xmlartisttag = new XMLElement(getthisartist);
          } catch (Exception e)
               {
                 println(e);
               }

         int numartisttag = xmlartisttag.getChildCount();
         println(numartisttag);
         if (numartisttag >= 1) {
             XMLElement artisttag = xmlartisttag.getChild(0);
             XMLElement kidartisttag = artisttag.getChild("name");
             personalcharts[i].tag = kidartisttag.getContent();
             println(personalcharts[i].artist+"  "+personalcharts[i].tag);
           }
        }
        sortbytag(artistnum);
   
       }

 
 }






old version that worked fine (but alas without multithreading):
Quote:

void artisttagloader(int numartists){
 for (int i = 0; i < numartists; i++)
 {
 
   Curstatus = "Searching Tags for "+personalcharts[i].artist;
   
       
   String getthisartist = convertencoding(personalcharts[i].artist);
   getthisartist = "http://ws.audioscrobbler.com/1.0/artist/"+getthisartist+"/toptags.xml";
   
   println(getthisartist);
   xmlartisttag = new XMLElement(this, getthisartist);
   
   int numartisttag = xmlartisttag.getChildCount();
   if (numartisttag >= 1) {
       XMLElement artisttag = xmlartisttag.getChild(0);
       XMLElement kidartisttag = artisttag.getChild("name");
       personalcharts[i].tag = kidartisttag.getContent();
       println(personalcharts[i].artist+"  "+personalcharts[i].tag);
     }
  }
  sortbytag(numartists);
 
}








tried using revision 136 but it somehow doesn't work. copied the revision 136 xml libraries over to my revision 135 but it didn't help.
Re: XMLElement inside multithreaded class?
Reply #1 - May 30th, 2008, 6:13pm
 
Try something like:
Code:
class CL_artisttagloader extends Thread{
XMLElement xmlartisttag;
PApplet parent;
CL_artisttagloader(Papplet _parent)
{
parent=_parent;
}

public void run() {
for (int i = 0; i < artistnum; i++)
{

Curstatus = "Searching Tags for "+personalcharts[i].artist;

String getthisartist = convertencoding(personalcharts[i].artist);
getthisartist = "http://ws.audioscrobbler.com/1.0/artist/"+getthisartist+"/toptags.xml";

println(getthisartist);
try {
xmlartisttag = new XMLElement(parent,getthisartist);
} catch (Exception e)
{
println(e);
}

int numartisttag = xmlartisttag.getChildCount();
println(numartisttag);
if (numartisttag >= 1) {
XMLElement artisttag = xmlartisttag.getChild(0);
XMLElement kidartisttag = artisttag.getChild("name");
personalcharts[i].tag = kidartisttag.getContent();
println(personalcharts[i].artist+" "+personalcharts[i].tag);
}
}
sortbytag(artistnum);

}


}

// and now you need to create your CL_artisttagloader with:

CL_artisttagloader loader;

void setup()
{
//normal setup...
CL_artisttagloader(this);
}
Re: XMLElement inside multithreaded class?
Reply #2 - May 30th, 2008, 7:19pm
 
thanks for the quick answer. sounds logical too. but i'm unsure as howto integrate the "Thread" into the setup.
Before i initialized (at least tried to Wink ) with:
Quote:
import processing.xml.*;
import java.lang.Thread;
Thread artisttagloaderThread;
void setup(){
 
 Thread artisttagloaderThread = new CL_artistloader();
   
}


and then the class.

now i tried:
Quote:
import processing.xml.*;
import java.lang.Thread;


CL_artisttagloader loader;
//Thread artisttagloaderThread; <- old version


void setup(){
 
 loader = new CL_artisttagloader(this);  
//error message: perhaps you wanted to use the overloaded version "CL_artisttagloader(?? _parent);" instead?


 //Thread artisttagloaderThread = new CL_artistloader(); <-- old version

   
}

//+++++++++++++++++++++++++++++++++++++++++++++++++


class CL_artisttagloader extends Thread{
XMLElement xmlartisttag;

PApplet parent;
 CL_artisttagloader(Papplet _parent)
 {
   parent=_parent;
 }

 public void run() {
     for (int i = 0; i < artistnum; i++)
     {
 
         Curstatus = "Searching Tags for "+personalcharts[i].artist;
         
         String getthisartist = convertencoding(personalcharts[i].artist);
         getthisartist = "http://ws.audioscrobbler.com/1.0/artist/"+getthisartist+"/toptags.xml";
       
         println(getthisartist);
         try {
           xmlartisttag = new XMLElement(parent, getthisartist);
          } catch (Exception e)
               {
                 println(e);
               }

         int numartisttag = xmlartisttag.getChildCount();
         println(numartisttag);
         if (numartisttag >= 1) {
             XMLElement artisttag = xmlartisttag.getChild(0);
             XMLElement kidartisttag = artisttag.getChild("name");
             personalcharts[i].tag = kidartisttag.getContent();
             println(personalcharts[i].artist+"  "+personalcharts[i].tag);
           }
        }
        sortbytag(artistnum);
   
       }

 
 }


but i get the error message "perhaps you wanted to use the overloaded version "CL_artisttagloader(?? _parent);" instead?"
i tried several things but i'm kinda stupid O_o
Re: XMLElement inside multithreaded class?
Reply #3 - May 30th, 2008, 9:20pm
 
There are a lot of bugs in your thread class. The first one: Papplet _parent should be PApplet _parent. And then a some of the variable will be used without initializing them (personalcharts, artistnum, Curstatus, xmlartisttag).
Re: XMLElement inside multithreaded class?
Reply #4 - May 30th, 2008, 9:37pm
 
thanks!

sorry, i pasted only the important parts, that's why those variables aren't initialized..my script is way too long to post here Cheesy
Page Index Toggle Pages: 1