Question

What I want to do is run this bot every 30 minutes. What it does it take a screenshot so that I can look at things. I tried using

    thread.sleep();

But that wasn't working for me. I set it to some small interval to see if it would actually work, and it just runs once and stops. I am new-ish to Java, but I haven't worked with the Java robot class much, or loops of any kind. Here is my class...

public class usiwa{


public static void main(String[] args) throws AWTException, IOException, InterruptedException{


    Robot bot = new Robot();
    Date date = new Date();
    Random ra = new Random();

    int x0 = MouseInfo.getPointerInfo().getLocation().x;
    int y0 = MouseInfo.getPointerInfo().getLocation().y;

    int x1 = ra.nextInt(1302 - 1224 + 1) + 1224;
    int y1 = ra.nextInt(80 - 70 + 1) + 70;

    int dx = x1 - x0;
    int dy = y1 - y0;

            // time in msecs
    int t = 1000;

    int res = Math.max(dx, dy);

    if(res > t) res = t;
    int d = t/res;

    float inv = (float) 1/(res - 1);
    float a = 0;

    long s = 0;
    long e = 0;
    s = System.currentTimeMillis();

    for(int i = 0; i < res; i++) {
        a += inv;

        bot.mouseMove(x0 + (int) (a*dx), y0 + (int) (a*dy));
        bot.delay(d);



    }
    e = System.currentTimeMillis();
    System.out.println("Total time: " + (float) (e - s)/1000);


    bot.mousePress(InputEvent.BUTTON1_MASK);
    bot.mouseRelease(InputEvent.BUTTON1_MASK);

    bot.delay(3000);

    Rectangle r = new Rectangle(0, 0, 1000, 1000);
    BufferedImage p = bot.createScreenCapture(r);
    DateFormat dateFormat = new SimpleDateFormat("MM_dd_yyyy-hh.mm.ss a");
    ImageIO.write(p, "png" , new File("C:/Users/Kalob_2/Desktop/Tracker/" + dateFormat.format(date) + ".png"));




}



}

All I want to do is make all of the actions above repeat every 30 minutes.

Thanks for any help.

Was it helpful?

Solution

Here is how you repeat a task. Wrap your existing code in this while loop:

while(true) {
    // do something
    Thread.sleep(1000); // 1 second, the parameter is miliseconds
}

Alternatively, create a cron job to run your code. The following will repeat every 30 minutes.

30 * * * *

OTHER TIPS

If you want to pause your thread you can do:

Thread.sleep(30*60*1000); \\ 30 minutes

But you are much better off using ScheduledExecutorService

public class usiwa {

     public static void main(String[] args) throws AWTException, IOException,
        InterruptedException {

        Robot bot = new Robot();
        Date date = new Date();
        Random ra = new Random();

        while (true) {
            int x0 = MouseInfo.getPointerInfo().getLocation().x;
            int y0 = MouseInfo.getPointerInfo().getLocation().y;

            int x1 = ra.nextInt(1302 - 1224 + 1) + 1224;
            int y1 = ra.nextInt(80 - 70 + 1) + 70;

            int dx = x1 - x0;
            int dy = y1 - y0;

            // time in msecs
            int t = 1000;

            int res = Math.max(dx, dy);

            if (res > t)
            res = t;
            int d = t / res;

            float inv = (float) 1 / (res - 1);
            float a = 0;

            long s = 0;
            long e = 0;
            s = System.currentTimeMillis();

            for (int i = 0; i < res; i++) {
                a += inv;

                bot.mouseMove(x0 + (int) (a * dx), y0 + (int) (a * dy));
                bot.delay(d);

            }
            e = System.currentTimeMillis();
            System.out.println("Total time: " + (float) (e - s) / 1000);

            bot.mousePress(InputEvent.BUTTON1_MASK);
            bot.mouseRelease(InputEvent.BUTTON1_MASK);

            bot.delay(3000);

            Rectangle r = new Rectangle(0, 0, 1000, 1000);
            BufferedImage p = bot.createScreenCapture(r);
            DateFormat dateFormat = new SimpleDateFormat(
                "MM_dd_yyyy-hh.mm.ss a");
            ImageIO.write(p, "png",
                new File("C:/Users/Kalob_2/Desktop/Tracker/"
                        + dateFormat.format(date) + ".png"));
        }

    }

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top