OmegaJeppe Posted January 2, 2022 Share Posted January 2, 2022 (edited) Hey everyone and happy new year! This might be simple, but i can't get my head around how i'm to stop/pause a thread, and start it again after calling a method. I have been trying the following example, and it's working to some point, but breaks eventually and the threads will fail to run. I have also been reading about wait() and notifyAll(), but haven't been able to get that to work. Does anyone have some insight on this topic, that might be willing so share a snippet or give some tips? AttackThread.interrupt(); MoveToRevenantsThread.interrupt(); teleport.run(); AttackThread = new Thread(attack); MoveToRevenantsThread = new Thread(moveToRevenants); Edited January 2, 2022 by OmegaJeppe shorter title Quote Link to comment Share on other sites More sharing options...
Gunman Posted January 2, 2022 Share Posted January 2, 2022 22 minutes ago, OmegaJeppe said: Hey everyone and happy new year! This might be simple, but i can't get my head around how i'm to stop/pause a thread, and start it again after calling a method. I have been trying the following example, and it's working to some point, but breaks eventually and the threads will fail to run. I have also been reading about wait() and notifyAll(), but haven't been able to get that to work. Does anyone have some insight on this topic, that might be willing so share a snippet or give some tips? AttackThread.interrupt(); MoveToRevenantsThread.interrupt(); teleport.run(); AttackThread = new Thread(attack); MoveToRevenantsThread = new Thread(moveToRevenants); Why are you making a new thread for what looks to be each task? Quote Link to comment Share on other sites More sharing options...
OmegaJeppe Posted January 2, 2022 Author Share Posted January 2, 2022 5 minutes ago, Gunman said: Why are you making a new thread for what looks to be each task? It's not every task, my main functionality is to always check for nearby enemies, and in that case teleport away. When enemies are nearby i have to control my other tasks/threads, and stop them so they dont take control over my mouse, which could cause problems to the teleporting part. Quote Link to comment Share on other sites More sharing options...
Gunman Posted January 2, 2022 Share Posted January 2, 2022 7 minutes ago, OmegaJeppe said: It's not every task, my main functionality is to always check for nearby enemies, and in that case teleport away. When enemies are nearby i have to control my other tasks/threads, and stop them so they dont take control over my mouse, which could cause problems to the teleporting part. Why don't you reverse it and make a thread that constantly scans for nearby enemies and then "pause" the main loop if one is so you can get away? That way you're only creating one new thread and you don't really need to do anything besides kill it on script exit 1 Quote Link to comment Share on other sites More sharing options...
OmegaJeppe Posted January 2, 2022 Author Share Posted January 2, 2022 3 minutes ago, Gunman said: Why don't you reverse it and make a thread that constantly scans for nearby enemies and then "pause" the main loop if one is so you can get away? That way you're only creating one new thread and you don't really need to do anything besides kill it on script exit I was trying that for a while, but i couldn't seem to find a way of taking control of the main main loop from the nearby enemies thread, and stopping it in the exact second a nearby enemy was approaching. The only thing i was able to do was to loop through the main script, and when it eventually finished what it was doing, it then received the request to stop - but then i was already dead. If you know how to do it the other way around, that would also be perfect. Quote Link to comment Share on other sites More sharing options...
Gunman Posted January 2, 2022 Share Posted January 2, 2022 1 minute ago, OmegaJeppe said: I was trying that for a while, but i couldn't seem to find a way of taking control of the main main loop from the nearby enemies thread, and stopping it in the exact second a nearby enemy was approaching. The only thing i was able to do was to loop through the main script, and when it eventually finished what it was doing, it then received the request to stop - but then i was already dead. If you know how to do it the other way around, that would also be perfect. Sounds like you're doing more than 1 interaction per cycle of the loop, probably why. And you could always call the script executor and pause it, forcing it to stop what it's doing, then resume after you're done. https://osbot.org/api/org/osbot/rs07/event/ScriptExecutor.html 1 Quote Link to comment Share on other sites More sharing options...
OmegaJeppe Posted January 2, 2022 Author Share Posted January 2, 2022 9 minutes ago, Gunman said: Sounds like you're doing more than 1 interaction per cycle of the loop, probably why. And you could always call the script executor and pause it, forcing it to stop what it's doing, then resume after you're done. https://osbot.org/api/org/osbot/rs07/event/ScriptExecutor.html Ahh interesting. So i would be able to call the script executor from the nearby enemies thread, pausing the main loop untill teleport is complete and then resuming it again? Quote Link to comment Share on other sites More sharing options...
Gunman Posted January 2, 2022 Share Posted January 2, 2022 9 minutes ago, OmegaJeppe said: Ahh interesting. So i would be able to call the script executor from the nearby enemies thread, pausing the main loop untill teleport is complete and then resuming it again? Not the cleanest of solutions, but yes, this will work. Quote Link to comment Share on other sites More sharing options...
OmegaJeppe Posted January 2, 2022 Author Share Posted January 2, 2022 6 minutes ago, Gunman said: Not the cleanest of solutions, but yes, this will work. Alright, thanks a lot man, really been struggling with this one for quite some time - great to get some other eyes on the issue, so i can move on. Quote Link to comment Share on other sites More sharing options...