Reputation: 58
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.
Upvotes: 1
Views: 122
Reputation: 949
Looking at this code, it seems you could rename WaitTillDone to WaitTillScreenShotDifferentIn4SecountSnapshots
As long as the image is the same as it was 4 seconds ago, it will re-enter the loop.
So if for example you enter your loop, after all of the changes have already been made, you will never exit it.
I think that what is going on here is that in a quarter of the cases Excel is too quick for you and finishes making changes before you enter your loop.
For example if your initial image is y, and you enter your do-loop at time 0:
This will be loop forever until the screenshot changes again:
time (s): Image
-1 y
0 x
1 x
2 x
3 x
4 x
This is likely what is occur most of the time:
time (s): Image
-1 y
0 y
1 y or x
2 y or x
3 y or x
4 x
Upvotes: 1