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 › Overloading exit()/super()
Page Index Toggle Pages: 1
Overloading exit()/super() (Read 858 times)
Overloading exit()/super()
Apr 23rd, 2010, 1:13pm
 
Hello,

Below is the code i had written to run a timer and save the data to an xml and an online database before exit. It works perfectly fine when i execute the code from processing but it doesn't work as I export it to window exe. As i run the exe it creates the xml but the code inside exit() is not executed as i close the exe.

It would be a great help if anyone can solve this problem

Many thanks.

/*Code: Using proXML for xml usage*/
   
   import proxml.*;
   import de.bezier.data.sql.*;

   PFont font;
   int currentTimer, seconds,x;
   XMLInOut xmlIO;
   proxml.XMLElement system;
   String uniquetemp, newunique;
   
   MySQL msql;
   
   void setup(){
     background(255, 204, 0);
     size(550,100);
     smooth();
     x=0;
     font = loadFont("Swiss721BT-BoldCondensedDType-24.vlw");
     xmlIO = new XMLInOut(this);
     
     String user     = "xxxx";
     String pass     = "xxxx";
     String database = "xxxx";
     
     msql = new MySQL( this, "xxxx", database, user, pass );
     
     
     try {
         xmlIO.loadElement("power_saver.xml");
         x=1;
     }
     catch (Exception e) {
       xmlEvent(new proxml.XMLElement("system"));
       proxml.XMLElement power = new proxml.XMLElement("power");
       system.addChild(power);
       newunique = str(int(random(255)))+str(int(random(255)))+str(int(random(255)))+str(int(random
(255)))+str(int(random(255)));
       power.addAttribute("uniqueid", newunique);  
       power.addAttribute("timer", int(millis()/1000));
       xmlIO.saveElement(system, "power_saver.xml");
       x=0;
     }
   }
   
   void draw(){
     textFont(font);
     background(255, 204, 0);
     currentTimer = int(millis()/1000);
     text("My system is on for : "+currentTimer+" seconds this session", 20, 50);
   }
   
   void xmlEvent(proxml.XMLElement element) {
     system = element;
     initPower();
   }
   
   void initPower() {
     //system.printElementTree(" ");
     proxml.XMLElement power;
     for(int i = 0; i < system.countChildren();i++){
     power = system.getChild(i);
     seconds = power.getIntAttribute("timer");
     uniquetemp = power.getAttribute("uniqueid");
     println(uniquetemp);
     }
   }
   
   void updatedb () {
     if (x==1) {
       proxml.XMLElement power;
       power = system.getChild(0);
       power.addAttribute("timer", seconds+currentTimer);
       seconds = seconds+currentTimer;
       xmlIO.saveElement(system, "power_saver.xml");
       if ( msql.connect() )
       {
           msql.execute( "update userdata set totaltime = "+seconds+" where uniqueid ="+uniquetemp);
       }
     }
    if (x==0)
     {
       proxml.XMLElement power;
       power = system.getChild(0);
       power.addAttribute("timer", currentTimer);
       xmlIO.saveElement(system, "power_saver.xml");
       if ( msql.connect() )
       {
           msql.execute( "insert into userdata(uniqueid,totaltime) values("+newunique+","+currentTimer+")" );
       }
     }
     
   }
   
   public void exit() {
     updatedb();
     super.exit();
   }
Re: Overloading exit()/super()
Reply #1 - Apr 23rd, 2010, 1:21pm
 
Try overloading stop() the same way as with exit
Re: Overloading exit()/super()
Reply #2 - Apr 23rd, 2010, 1:30pm
 
thanks John,

I tried with stop as well, but had the same result. Sad
Re: Overloading exit()/super()
Reply #3 - Apr 23rd, 2010, 2:07pm
 
stop() isn't guarantee to be called...

Perhaps use addWindowListener on the frame variable, with the windowClosing callback.
Re: Overloading exit()/super()
Reply #4 - Apr 23rd, 2010, 2:37pm
 
thanks PhiLho,

It worked great..many thanks Smiley
Page Index Toggle Pages: 1