Thread Types In Ubot

Ubot Studio
One of the most useful and powerful features in Ubot Studio is that of multi-threading. Take a spin with us as we look at the three different types of threads in Ubot and how you can create some really useful code to make your automation efforts more simplified and running fast.

Multi Threading In Ubot

Lets dive right in this time and take a look at the three types of threads in Ubot along with a cool example to demonstrate the concepts.

In Ubot, there are three types of threads:

  1. Your bot's main thread
  2. The UI HTML thread
  3. Event threads

Now here's the code to study:

<pre>
comment("------------- UI HTML thread ----------------")
ui stat monitor("<script type=\"text/javascript\">window.onload = ubot.runScript(\'MainOnLoad()\')</script>", "")
ui stat monitor("Run state", #runningState)
ui stat monitor("Test variable state:", #testvar)
ui stat monitor("Browser run state:", #browserRunningState)
ui button("Start Test Variable Spin") {
    set(#runningState, "On", "Global")
    MainUI()
}
ui button("Stop Test Variable Spin") {
    set(#runningState, "Off", "Global")
    stop script
}
ui button("Start browser") {
    set(#browserRunningState, "On", "Global")
    MainUI()
}
ui button("Stop browser") {
    set(#browserRunningState, "Off", "Global")
    stop script
}
comment("------------- Main UI thread ----------------")
define MainUI {
    thread {
        loop while($comparison(#runningState, "=", "On")) {
            wait(1)
            increment(#testvar)
        }
    }
}
comment("------------- Event thread ----------------")
define MainOnLoad {
    thread {
        set(#browserRunningState, "On", "Global")
        loop while($comparison(#browserRunningState, "=", "On")) {
            clear cookies
            navigate("http://www.yahoo.com", "Wait")
            wait(5)
        }
    }
}
comment("------------- Main bot thread ----------------")
set(#testvar, 0, "Global")
set(#runningState, "Off", "Global")
</pre>

Copy, paste and run this code within Ubot 4.

You should see in your UI panel, the current running state of the bot, the state of a loop counter, the state of the embedded browser and several start and stop buttons to turn on and off each.

Play with the buttons as well as the main Run and Stop buttons in Ubot.

Main bot thread

The main thread for the bot is executed whenever the user presses the Ubot Run button. It isn't explicitly labeled as a thread statement but it does get executed. This is where you make calls to the high level processing logic of your bot.

UI HTML Thread

Go ahead and turn off the bot by pressing the Stop button. Now, click on the Start Test Variable Spin button. Notice that the test variable state is being updated.

But the bot is off you say.

It is, the main thread of the bot is off, but the UI HTML thread isn't. The UI panel is a component all to itself. It doesn't have to interact with the browser nor be a part of the bot's main thread. This is a perfect place to put code for doing non-related browser processing.

Event Threads

For lack of a better term, I chose event threads.

One of the Ubot customers discovered an interesting way to assign a UI stat monitor to the ubot javascript interpreter.

ui stat monitor("<script type=\"text/javascript\">window.onload = ubot.runScript(\'MainOnLoad()\')</script>", "")

When our bot is executed, this javascript snippet will be executed allowing us to register as a listener to a Javascript onload window event.. I have wired it up to execute a command which contains a looping thread. It will continuously surf to a website until we click the Stop browser button.

Notice that if you attempt to click on the start browser button, the browser no longer navigates. This is because we exited out of the thread loop. In order to get back into the event thread, the onload Javascript event must trigger again. This is because I set the browserRunningState varaible inside the thread and just outside the loop. Hit the browser refresh button and the OnLoad event is again triggered.

Summary

So you have seen three different areas within Ubot where you can creating threading units. The main thread bot which is implicitly invoked, the UI HTML panel, and in events.


ASO ad


Filed under: