Conditional Threads?
in
Programming Questions
•
1 year ago
Hi-
I am in the processing of making my code run faster and smoother as I have several sections of identical code in my program because I can not pass the values of variables up the chain. So, I am trying to create some threads that only run when a key is pressed or when a mouse is clicked...Would it be more efficient to create a thread or something else? Also how would I go about adding the conditional part? In the example below I believe I would alter the run() command but so far I have been unsuccessful in my attempts.
Thank you~
I am in the processing of making my code run faster and smoother as I have several sections of identical code in my program because I can not pass the values of variables up the chain. So, I am trying to create some threads that only run when a key is pressed or when a mouse is clicked...Would it be more efficient to create a thread or something else? Also how would I go about adding the conditional part? In the example below I believe I would alter the run() command but so far I have been unsuccessful in my attempts.
- class TxtChange extends Thread {
boolean active;//running status
PApplet p;// to run serial ports as well
TxtChange() {//initial setting
active = false;
}
void start() {//start command
active = true;
super.start();
}
void run () {//to keep running this makes a loop how to get conditional
while(true){
if(active){
execute();
}else
break;
}
}
TxtChange(PApplet p) {
this.p = p; // store the reference to PApplet
active = false;
}
void execute() {// what it does
if(keyPressed){
if (mouseX>=406 && mouseX<=586 && mouseY>=45 && mouseY<=70){
if(key==BACKSPACE){
Additional = Additional.substring(0, Additional.length()-1);
}
else if(key==DELETE){
Additional = "";
}
else if(keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT){
Additional = Additional + key;
}
}
else if (mouseX>=113 && mouseX<=293 && mouseY>=45 && mouseY<=70){
if(key==BACKSPACE){
Commandt = Commandt.substring(0, Commandt.length()-1);
}
else if(key==DELETE){
Commandt = "";
}
else if(keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT){
Commandt = Commandt + key;
}
}
}
delay(80);//need to pause to not take all the processor memory
}
boolean isActive() {// no clue
return active;
}
void quit() {//how to end it
active = false;
interrupt();
}
}
Thank you~
1