You are here

Using infinite loop in a button click event

foxmain

November 2, 2014 - 3:12am, 1058 views

Hello

I am working with a terminal TC65.I have to fire an infinitive loop inside a Thread as soon as I pressed a push button thru GPIO.
I can stopped the loop without any problems with another button but I can't control the start of the application.BTW I am running the loop in a thread and I also know the issue comes from "while(true)" but I need to use it so the loop begins without waiting for the event.I'll be very grateful if someone helps me to find the solution to the problem

 class PortListener extends Thread implements InPortListener {      
    
     }
        
    
    public synchronized void portValueChanged(int portValue) {
    
     //the process should start
       if (portValue==1){
          System.out.println("Button Pressed GPIO4:"+ portValue);       
        // I have tested it with different logical flags without luck
              
        }
                                
      //the process should stop                       
                     else if ((portValue==2)){
                      System.out.println("Button Pressed GPIO6:"+ portValue);
                         stop();
                     
                     }
                     
    }
           
   public  synchronized void stop() {
    terminated = true;
     
  }
  
     //processing core
        
        public void run(){
    try{     
            
            while (true){
        
            {
               Thread.sleep(1000);
                cont++;
                         if(){
                        value=cont;
                        System.out.println("Total:" + value);
                        }
                                              
                          if(terminated)
                           break;
              
           }
              
            }  
             }catch(InterruptedException e) {
                  e.printStackTrace();
            }  
        }  
     
        
 }