crazywizard94 Posted January 11, 2023 Share Posted January 11, 2023 I'm an experienced .NET developer with some Java knowledge and I'm new to the OSBot library. I've been having trouble to get my script to sleep before the end of a loop. Cause scenario: wait until a widget is visible. In this case I'd just wait 500 ms but it seems like none of my efforts work.. Whether I use wait(500) or sleep(500) the execution just goes to the next line immediately. I have a hunch this is to do with the fact that sleep methods only stop the thread from which they were called. And java applications can spin up as many threads as they want so it seems the entire concept of wait and sleep is very experimental. Is it bad practise to use these methods? Should you try to only sleep when you return from the loop function? Quote Link to comment Share on other sites More sharing options...
ExtraBotz Posted January 11, 2023 Share Posted January 11, 2023 Quote Link to comment Share on other sites More sharing options...
Gunman Posted January 11, 2023 Share Posted January 11, 2023 2 hours ago, crazywizard94 said: I'm an experienced .NET developer with some Java knowledge and I'm new to the OSBot library. I've been having trouble to get my script to sleep before the end of a loop. Cause scenario: wait until a widget is visible. In this case I'd just wait 500 ms but it seems like none of my efforts work.. Whether I use wait(500) or sleep(500) the execution just goes to the next line immediately. I have a hunch this is to do with the fact that sleep methods only stop the thread from which they were called. And java applications can spin up as many threads as they want so it seems the entire concept of wait and sleep is very experimental. Is it bad practise to use these methods? Should you try to only sleep when you return from the loop function? Go take the scripting crash course bro https://osbot.org/forum/topic/115124-explvs-scripting-101/ Quote Link to comment Share on other sites More sharing options...
Malcolm Posted January 11, 2023 Share Posted January 11, 2023 easy public static void main(String[] args) { conditionalSleep(() -> false, 5_000); } public static void conditionalSleep(BooleanSupplier condition, long sleepTime) { final long startTime = System.currentTimeMillis(); do { if (System.currentTimeMillis() >= startTime + sleepTime) { break; } try { Thread.sleep(10); } catch (InterruptedException e) { } } while (!condition.getAsBoolean()); } Quote Link to comment Share on other sites More sharing options...
Khaleesi Posted January 11, 2023 Share Posted January 11, 2023 7 hours ago, crazywizard94 said: I'm an experienced .NET developer with some Java knowledge and I'm new to the OSBot library. I've been having trouble to get my script to sleep before the end of a loop. Cause scenario: wait until a widget is visible. In this case I'd just wait 500 ms but it seems like none of my efforts work.. Whether I use wait(500) or sleep(500) the execution just goes to the next line immediately. I have a hunch this is to do with the fact that sleep methods only stop the thread from which they were called. And java applications can spin up as many threads as they want so it seems the entire concept of wait and sleep is very experimental. Is it bad practise to use these methods? Should you try to only sleep when you return from the loop function? for a static sleep you can just use sleep, not really recommended since a bit of lag can mess up your script pretty quick. And you don't wanna sleep fro to long, bcs that will make the script really slow^^ You can use conditional sleeps instead https://osbot.org/api/org/osbot/rs07/utility/ConditionalSleep.html For example: new ConditionalSleep( 5000, 100) { @Override public boolean condition() throws InterruptedException { return script.myPlayer().isAnimating(); } }.sleep(); Sleeps for a maximum of 5000ms OR until the player is animating. This will check every 100ms Quote Link to comment Share on other sites More sharing options...
crazywizard94 Posted January 11, 2023 Author Share Posted January 11, 2023 8 hours ago, Malcolm said: easy public static void main(String[] args) { conditionalSleep(() -> false, 5_000); } public static void conditionalSleep(BooleanSupplier condition, long sleepTime) { final long startTime = System.currentTimeMillis(); do { if (System.currentTimeMillis() >= startTime + sleepTime) { break; } try { Thread.sleep(10); } catch (InterruptedException e) { } } while (!condition.getAsBoolean()); } Okay so with the conditionalSleep() you just supply the condition? I'm gonna assume this is thread-safe. Cheers mate 13 hours ago, ExtraBotz said: Lol I've seen that series but cocky me decided to start on Episode 3 Quote Link to comment Share on other sites More sharing options...