time schedule rate rpoblem
Hi,
i have a program which should will print screen every 0.1s but when i compile and run, it print screen 2seconds 1 times but not print once/0.1seconds . below is my code
import java.awt.Toolkit;
import java.util.Timer;
import java.util.TimerTask;
import java.awt.image.BufferedImage;
import java.awt.Rectangle;
import java.awt.Dimension;
import java.awt.Robot;
import java.io.File;
import javax.imageio.ImageIO;
/**
* Schedule a task that executes once every second.
*/
public class Main {
Toolkit toolkit;
Timer timer;
public Main() {
toolkit = Toolkit.getDefaultToolkit();
timer = new Timer();
timer.scheduleAtFixedRate(new RemindTask(), 0, //initial delay
100); //execute the code 1 seconds 1 time
}
class RemindTask extends TimerTask {
public void run() {
try {
Toolkit tool = Toolkit.getDefaultToolkit();
Dimension d = tool.getScreenSize();
Rectangle rect = new Rectangle(1024,768);
Robot robot = new Robot();
Thread.sleep(2000);
File f = new File("screenshot.jpg");
BufferedImage img = robot.createScreenCapture(rect);
ImageIO.write(img,"jpeg",f);
} catch(Exception e){}
}//close public void run
}//close class Reminder
public static void main(String args[]) {
new Main();
}//close public void main
}//close class main
is anyone what is the error/bug there that make the time schedule works incorrectly ?
thanks in advance for the reply
Re: time schedule rate rpoblem
I think it is because you have a two seconds delay in you RemindTask.run() method.
Code:
Thread.sleep(2000); // two seconds
Re: time schedule rate rpoblem
thanks so much . so i neglect that part .. very thanks for your help ..