1

I have a method that has to take a picture of the screen every 0.5 seconds and saves the image in a place on the HD. But I need him to run between 11:55 a.m. and 4:55 p.m. to 5:00 p.m.

I just got to start the Task and I could not stop it.

My doubt is:

How could I schedule, for the thread to run only within a certain period of time.

public class Main {

private Toolkit a = Toolkit.getDefaultToolkit();
private Dimension screenSize = a.getScreenSize();
private Rectangle screenLimit = new Rectangle(screenSize);
private Robot robot;

private File file;

BufferedImage img;


private final static int TWO_AM = 11;
private final static int ZERO_MINUTES = 28;



private static Date getTomorrowMorning1145AM(){

    Date date2am = new java.util.Date(); 
       date2am.setHours(TWO_AM); 
       date2am.setMinutes(ZERO_MINUTES); 

       return date2am;
  }



public Main() {

    String path = "c:\\print\\"+System.getProperty("user.name")+"\\";

    try {
        robot = new Robot();
        file = new File(path);
    } catch (AWTException e1) {
        e1.printStackTrace();
    }

    if(!file.exists()){
        file.mkdirs();
    }

    TimerTask tt = new TimerTask() {

        @Override
        public void run() {

            try {
                tirarPrint(path+"print_" + new Date().getTime() + ".jpg");
            } catch (IOException | AWTException e) {
                e.printStackTrace();
            }

        }
    };

    Timer t = new Timer();

    t.schedule(tt, 0, 500);

}

private void tirarPrint(String caminho) throws AWTException, IOException {

    img = robot.createScreenCapture(screenLimit);
    ImageIO.write(img, "jpg", new File(caminho));

 }

public static void main(String[] args) {
    new Main(); 
  }
}
Phrxn
  • 69
  • 7

2 Answers2

0

You could use windows task scheduler to run the program on a schedule, or if you want the program to control it, you could have a loop that runs continuously, sleeping for 30 seconds then checks the time, and runs the code if it is the desired time.

drone6502
  • 433
  • 2
  • 7
0

For this task I would use two ExecutorServices.

One checks every minute if it is the right time frame and the second executes your program code every 0.5s.

http://tutorials.jenkov.com/java-util-concurrent/executorservice.html

public static ScheduledExecutorService timerFrameExecutor = Executors.newSingleThreadScheduledExecutor();
public static ScheduledExecutorService shortTimeExecutor = Executors.newSingleThreadScheduledExecutor();

public static void main(String[] args) {
    Runnable timerFrameRunnable = new Runnable() {

        @Override
        public void run() {
            if (inTimePeriond() == false) {
                shortTimeExecutor.shutdown();
            } else {
                if (shortTimeExecutor.isShutdown()) {
                    Runnable shortTimeRunnable = new Runnable() {

                        @Override
                        public void run() {
                            // do your stuff
                        }
                    };
                    shortTimeExecutor.scheduleAtFixedRate(shortTimeRunnable, 0, 500, TimeUnit.MILLISECONDS);
                }
            }
        }
    };
    timerFrameExecutor.scheduleAtFixedRate(timerFrameRunnable, 0, 1, TimeUnit.MINUTES);
}

To implement the inTimePeriod() function see this question: Check if a given time lies between two times regardless of date

secustor
  • 3,001
  • 2
  • 14
  • 20