I have created an program that automates certain functions at my place of business but I'm trying to dummy proof it so it cannot go wrong and I have encountered an error that should not be occurring.
Basically what my method is doing is waiting until the spreadsheet has stopped being populated and auto formatted so I can do some other stuff with it. All this method has to do is take a screen shot, wait, take another screen shot and if they are the same then continue, if not wait some more.
here are the methods.
private boolean WaitTillDone() {
BufferedImage image1;
BufferedImage image2;
image1 = siri.createScreenCapture(new Rectangle(0,0,width,height-80));
wait(4000);
image2 = siri.createScreenCapture(new Rectangle(0,0,width,height-80));
boolean same = bufferedImagesEqual(image1,image2);
return same;
}
public boolean bufferedImagesEqual(BufferedImage img1, BufferedImage img2) {
if (img1.getWidth() == img2.getWidth() && img1.getHeight() == img2.getHeight() ) {
for (int x = 0; x < img1.getWidth(); x++) {
for (int y = 0; y < img1.getHeight(); y++) {
if (img1.getRGB(x, y) != img2.getRGB(x, y) ) return false;
}
}
}
else {
return false;
}
return true;
}
Here is the loop that this is being called in.
do{
running = WaitTillDone();
wait(800);
}while(running);
The program loops ok but sometimes when the images are not the same and it has to wait it will enter an "infinite" loop. I say "infinite" because without any user input it will not continue. However if I press any arrow button or enter just to move the selected box in excel so that the images will be different it will continue along with no problems. So i was wondering if there was anything that I was doing wrong (besides calling a wait (a Thread.sleep) in a loop) that would cause my program to have this logic error.
EDIT: This problem is not occurring every time, only about 1/4th of the time. This problem is resolved, the mistake was the do-while condidtion. It was supposed to be
do{
running = WaitTillDone();
wait(800);
}while(!running);
Thank you all for your help.