Jump to content

Troubles with ConditionalSleep


Recommended Posts

Posted

Been trying to use a ConditionalSleep to check when the trade window opens up, but it seems to just timeout before it even realises that it's actually trading.

 

log("before cond");
log(trade.isCurrentlyTrading()); // returns false, as expected
condSleep(10000, 200, trade.isCurrentlyTrading());
log("after cond");
log(trade.isCurrentlyTrading()); // returns true, as expected

// also in the class
	private void condSleep(int timeout, int delay, boolean b) {
		new ConditionalSleep(timeout, delay) {
			@Override
			public boolean condition() throws InterruptedException {
				return b;
			}
		}.sleep();
	}

 

Any help would be greatly appreciated!

Posted

You're passing the boolean as false condSleep(10000, 200, trade.isCurrentlyTrading()); 

This means it will sleep for 10 seconds every time. You need to pass a boolean supplier like so:

log("before cond");
log(trade.isCurrentlyTrading()); // returns false, as expected
condSleep(10000, 200, () -> trade.isCurrentlyTrading());
log("after cond");
log(trade.isCurrentlyTrading()); // returns true, as expected

// also in the class
	private void condSleep(int timeout, int delay, BooleanSupplier b) {
		new ConditionalSleep(timeout, delay) {
			@Override
			public boolean condition() throws InterruptedException {
				return b.asBoolean();
			}
		}.sleep();
	}

 

Posted
1 minute ago, nosepicker said:

You're passing the boolean as false condSleep(10000, 200, trade.isCurrentlyTrading()); 

This means it will sleep for 10 seconds every time. You need to pass a boolean supplier like so:


log("before cond");
log(trade.isCurrentlyTrading()); // returns false, as expected
condSleep(10000, 200, () -> trade.isCurrentlyTrading());
log("after cond");
log(trade.isCurrentlyTrading()); // returns true, as expected

// also in the class
	private void condSleep(int timeout, int delay, BooleanSupplier b) {
		new ConditionalSleep(timeout, delay) {
			@Override
			public boolean condition() throws InterruptedException {
				return b.asBoolean();
			}
		}.sleep();
	}

 

I'm not entirely sure what you're talking about tbh, but the method I use works fine for things such as bank.isOpen(), so I'm still at a loss of why it works for some booleans and not others?

Posted (edited)
18 minutes ago, Slut said:

I'm not entirely sure what you're talking about tbh, but the method I use works fine for things such as bank.isOpen(), so I'm still at a loss of why it works for some booleans and not others?

new ConditionalSleep(timeout, delay) {
			@Override
			public boolean condition() throws InterruptedException {
				return b.asBoolean();
			}
		}.sleep();

This little thing here. It says sleep for a maximum time of TIMEOUT and check every DELAY time if the CONDITION is true.

Now when you pass 10000, 200, false as parameters it means that, hey sleep for 10 seconds and check every 200ms if true == false, which will never be true, so it will sleep for 10 seconds. The boolean you pass is a "constant" let's say.

Bolean supplier "fetches" the true/false result from the CONDITION whenever it's invoked. So it will check if it's true/false every 200ms in your case.

Edited by nosepicker
  • Like 1
Posted
4 minutes ago, nosepicker said:

new ConditionalSleep(timeout, delay) {
			@Override
			public boolean condition() throws InterruptedException {
				return b.asBoolean();
			}
		}.sleep();

This little thing here. It says sleep for a maximum time of TIMEOUT and check every DELAY time if the CONDITION is true.

Now when you pass 10000, 200, false as parameters it means that, hey sleep for 10 seconds and check every 200ms if true == false, which will never be true, so it will sleep for 10 seconds. The boolean you pass is a "constant" let's say.

Bolean supplier "fetches" the true/false result from the CONDITION whenever it's invoked. So it will check if it's true/false every 200ms in your case.

Ah I see! Thanks for your thorough explanation, really helped :)

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...