Jump to content

Troubles with ConditionalSleep


Hel

Recommended Posts

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!

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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 :)

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