Jump to content

Wait, Sleep and Threads explained please?


crazywizard94

Recommended Posts

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? 

Link to comment
Share on other sites

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/

Link to comment
Share on other sites

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());
}
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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 😁 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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