Jump to content
UBot Underground

[FREE] - Multi-Threading with Status


Recommended Posts

Hi UBotters!

 

Just thought I would share my script I made today! Some of you might find it useful for your bots!

 

Basically It does what it says in the title "Multi-Threading with Status" in the UI!

 

Here is the code:

ui html panel("<!DOCTYPE html>
<html lang=\"en\">
  <head>
    <meta charset=\"utf-8\">
    <meta name=\"generator\" content=\"CoffeeCup HTML Editor (www.coffeecup.com)\">
    <meta name=\"dcterms.created\" content=\"Thu, 24 Oct 2013 11:19:20 GMT\">
    <meta name=\"description\" content=\"\">
    <meta name=\"keywords\" content=\"\">
    <title></title>
    
    <!--[if IE]>
    <script src=\"http://html5shim.googlecode.com/svn/trunk/html5.js\"></script>
    <![endif]-->
  </head>
  <body>
<select variable=\"#Threads\">
<option>Threads</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
</select>

<input type=\"number\" step=\"1\" variable=\"#Loops\" placeholder=\"Total Loops\" />
<button onclick=\"ubot.runScript(\'Stop()\')\">Stop</button>
<br>
<div style=\"width:350px;height:350px; overflow:scroll;\">
<span fillwith=\"innertext\" variable=\"#Stat\"></span>
</div>
  </body>
</html>", 400)
define $Find Free Thread {
    set(#Found, $false, "Local")
    set(#Row, 0, "Local")
    loop while($comparison(#Found, "=", $false)) {
        Check Stop()
        if($comparison($table total rows(&Free_Threads), "=", #Row)) {
            then {
                set(#Row, 0, "Local")
            }
            else {
            }
        }
        if($comparison($table cell(&Free_Threads, #Row, 0), "!=", $nothing)) {
            then {
                set(#Found, $true, "Local")
                return($table cell(&Free_Threads, #Row, 0))
            }
            else {
            }
        }
        increment(#Row)
    }
}
Start()
define Stop {
    set(#Stop, $true, "Global")
}
define Check Stop {
    if($comparison(#Stop, "=", $true)) {
        then {
            stop script
        }
        else {
        }
    }
}
define Set Status(#Thread, #Status) {
    set(#Thread, #Thread, "Local")
    set(#Status, #Status, "Local")
    set table cell(&Threads, #Thread, 0, "[THREAD {#Thread}] {#Status}")
    set(#Stat, &Threads, "Global")
}
define Start {
    set(#Stop, $false, "Global")
    clear table(&Threads)
    clear table(&Free_Threads)
    set(#Thread_Count, 0, "Global")
    loop(#Threads) {
        set table cell(&Free_Threads, #Thread_Count, 0, #Thread_Count)
        increment(#Thread_Count)
    }
    comment("Start Threading...")
    set(#Threads_Open, 0, "Global")
    loop(#Loops) {
        Check Stop()
        loop while($comparison(#Threads_Open, ">=", #Thread_Count)) {
        }
        increment(#Threads_Open)
        wait(0.5)
        thread {
            in new browser {
                Open Thread($Find Free Thread())
                decrement(#Threads_Open)
            }
        }
    }
    loop while($comparison(#Threads_Open, "!=", 0)) {
        wait(1)
    }
    alert("complete")
    set(#Stop, $true, "Global")
}
define Open Thread(#Thread) {
    set(#Thread, #Thread, "Local")
    set table cell(&Free_Threads, #Thread, 0, "")
    Registration(#Thread)
    set table cell(&Free_Threads, #Thread, 0, #Thread)
}
divider
define Registration(#Thread) {
    set(#Thread, #Thread, "Local")
    set(#Waiting, 10, "Local")
    loop(#Waiting) {
        wait(1)
        Set Status(#Thread, "Waiting... {#Waiting}")
        decrement(#Waiting)
    }
    Check Stop()
    Set Status(#Thread, "Waiting again...")
    wait(5)
    Set Status(#Thread, "Complete!")
}

I have also attached the .ubot file....

 

Hope you like!!

 

and if you are feeling really friendly you can donate here:

 

http://lazybots.com/donate.html

 

Carl  ;)

Threading With Status.ubot

  • Like 2
Link to post
Share on other sites

Maybe you should just know that this threading approach is not safe and it doesn't work as expected (it will be loosing threads if you run a lot of threads for longer period) as described here.

 

The problem is in this 2 lines:

set(#Threads_Open, 0, "Global")

thread {
    decrement(#Threads_Open)
}


Link to post
Share on other sites
  • 4 weeks later...
  • 2 weeks later...

Hi, Sorry I just noticed this thread. 

 

I have added the threads counter to the script, its much faster launching the threads!

ui html panel("<!DOCTYPE html>
<html lang=\"en\">
  <head>
    <meta charset=\"utf-8\">
    <meta name=\"generator\" content=\"CoffeeCup HTML Editor (www.coffeecup.com)\">
    <meta name=\"dcterms.created\" content=\"Thu, 24 Oct 2013 11:19:20 GMT\">
    <meta name=\"description\" content=\"\">
    <meta name=\"keywords\" content=\"\">
    <title></title>
    
    <!--[if IE]>
    <script src=\"http://html5shim.googlecode.com/svn/trunk/html5.js\"></script>
    <![endif]-->
  </head>
  <body>
<select variable=\"#Threads\">
<option>Threads</option>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
<option>6</option>
<option>7</option>
<option>8</option>
<option>9</option>
<option>10</option>
<option>11</option>
<option>12</option>
<option>13</option>
<option>14</option>
<option>15</option>
<option>16</option>
<option>17</option>
<option>18</option>
<option>19</option>
<option>20</option>
<option>21</option>
<option>22</option>
<option>23</option>
<option>24</option>
<option>25</option>
<option>26</option>
<option>27</option>
<option>28</option>
<option>29</option>
<option>30</option>
</select>

<input type=\"number\" step=\"1\" variable=\"#Loops\" placeholder=\"Total Loops\" />
<button onclick=\"ubot.runScript(\'Stop()\')\">Stop</button>
<br>
<div style=\"width:350px;height:350px; overflow:scroll;\">
<span fillwith=\"innertext\" variable=\"#Stat\"></span>
</div>
  </body>
</html>", 400)
define $Find Free Thread {
    set(#Found, $false, "Local")
    set(#Row, 0, "Local")
    loop while($comparison(#Found, "=", $false)) {
        Check Stop()
        if($comparison($table total rows(&Free_Threads), "=", #Row)) {
            then {
                set(#Row, 0, "Local")
            }
            else {
            }
        }
        if($comparison($table cell(&Free_Threads, #Row, 0), "!=", $nothing)) {
            then {
                set(#Found, $true, "Local")
                return($table cell(&Free_Threads, #Row, 0))
            }
            else {
            }
        }
        increment(#Row)
    }
}
Start()
define Start {
    set(#Stop, $false, "Global")
    clear table(&Threads)
    clear table(&Free_Threads)
    set(#Thread_Count, 0, "Global")
    loop(#Threads) {
        set table cell(&Free_Threads, #Thread_Count, 0, #Thread_Count)
        increment(#Thread_Count)
    }
    comment("Start Threading...")
    set(#Threads_Open, $plugin function("Threads Counter.dll", "threads counter", "reset"), "Global")
    loop(#Loops) {
        Check Stop()
        loop while($comparison(#Threads_Open, ">=", #Thread_Count)) {
            wait(0.1)
        }
        set(#Threads_Open, $plugin function("Threads Counter.dll", "threads counter", "increment"), "Global")
        thread {
            Open Thread($Find Free Thread())
        }
    }
    loop while($comparison(#Threads_Open, "!=", 0)) {
        wait(1)
    }
    alert("complete")
    set(#Stop, $true, "Global")
}
define Open Thread(#Thread) {
    set(#Thread, #Thread, "Local")
    set table cell(&Free_Threads, #Thread, 0, "")
    in new browser {
        Registration(#Thread)
    }
    set table cell(&Free_Threads, #Thread, 0, #Thread)
    set(#Threads_Open, $plugin function("Threads Counter.dll", "threads counter", "decrement"), "Global")
}
define Stop {
    set(#Stop, $true, "Global")
}
define Check Stop {
    if($comparison(#Stop, "=", $true)) {
        then {
            stop script
        }
        else {
        }
    }
}
define Set Status(#Thread, #Status) {
    set(#Thread, #Thread, "Local")
    set(#Status, #Status, "Local")
    set table cell(&Threads, #Thread, 0, "[THREAD {#Thread}] {#Status}")
    set(#Stat, &Threads, "Global")
}
divider
define Registration(#Thread) {
    set(#Thread, #Thread, "Local")
    set(#Waiting, 10, "Local")
    loop(#Waiting) {
        wait(1)
        Set Status(#Thread, "Waiting... {#Waiting}")
        decrement(#Waiting)
    }
    Check Stop()
    Set Status(#Thread, "Waiting again...")
    wait(5)
    Set Status(#Thread, "Complete!")
}

Hope you like!

  • Like 1
Link to post
Share on other sites

Any idea how to use your threaded status example and pull data from a table using a global variable (#row) that increments after loading local var?

 

When I try and increment the #row after loading the local variables for the thread, some of the threads are pulling the same data?

 

I know there is a thread safe container plugin that is supposed to help with that, but when I tried that, it seemed to break the threaded status part, only showed 1 thread running?

 

Hope that makes sense.

Link to post
Share on other sites

What I tend to do is set the local variable and increment it straight away like so:

ui stat monitor("Local Stat:", #Stat)
set(#Stat, "", "Global")
set(#Row, 0, "Global")
loop(10) {
    thread {
        Run Thread()
    }
}
define Run Thread {
    set(#Row_Local, #Row, "Local")
    set(#Stat, "{#Stat} /{#Row_Local}/", "Global")
    increment(#Row)
}

Hope that helps!

 

Carl

Link to post
Share on other sites

Hmmm... what you suggested is essentially what I was doing.

 

When I took your code and ran it a few times, one of the results was /0 /0 /0 /1 /1 /1 /2 /3 /4 /4

But then I ran it a few more times and it seemed to get normal, and then a few more times and I got a /0 /0 /1 /2 /3 /4 /5 /6 /7 /8

 

Very confusing to me. 

 

Then i added a wait for .1 after the thread, and it seemed to make it more consistent.

 

Anyhow, I'll keep playing with it -- Thanks again!

Link to post
Share on other sites

Hmmm... what you suggested is essentially what I was doing.

 

When I took your code and ran it a few times, one of the results was /0 /0 /0 /1 /1 /1 /2 /3 /4 /4

But then I ran it a few more times and it seemed to get normal, and then a few more times and I got a /0 /0 /1 /2 /3 /4 /5 /6 /7 /8

 

Very confusing to me. 

 

Then i added a wait for .1 after the thread, and it seemed to make it more consistent.

 

Anyhow, I'll keep playing with it -- Thanks again!

 

Yes, a wait is probably needed before launching there threads. Im not sure if you could use another the thread counter for this. Ill have to give it a try...

Link to post
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.

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.

Loading...
×
×
  • Create New...