Using infinite loop in a button click event | Telit Cinterion IoT Developer Community
November 2, 2014 - 3:12am, 3264 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();
}
}
}
The task should be run periodically until the end with a new event.I can't control the starting of the task with "While(true)". Is Timer class the answer?
Hi Foxmain,
If you can stop the application by caching GPIO6 button pressed I assume that you have created the InPort instance and added the both pins and the listener to it. If so you should be able to catch in the portValueChanged() method both GPIO6 and GPIO4 buttons pressed.
I can't see where you start your thread. Please also take into consideration that the Thread instance can be started only once in a lifetime.
Timer and TimerTask are useful when you want to schedule the particular task to be started with a certain delay or to be started periodically with the fixed delay or the fixed rate.
Regards,
Bartłomiej