Jump to content

Wait, Sleep and Threads explained please?


Recommended Posts

Posted

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? 

Posted
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/

Posted

easy :doge:

 

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());
}
Posted
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

Posted
8 hours ago, Malcolm said:

easy :doge:

 

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 😁 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...