# How to create a thread with scripts

**URL:** https://forum.universal-robots.com/t/how-to-create-a-thread-with-scripts/24724
**Category:** Technical Questions
**Created:** [October 31, 2022, 2:49pm UTC](https://forum.universal-robots.com/t/how-to-create-a-thread-with-scripts/24724 "2022-10-31T14:49:42Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Elettrotec\_AC14](https://avatars.discourse-cdn.com/v4/letter/e/74df32/32.png) [@Elettrotec\_AC14](https://forum.universal-robots.com/u/Elettrotec_AC14)
#### Post date: [October 31, 2022, 2:49pm UTC](https://forum.universal-robots.com/t/how-to-create-a-thread-with-scripts/24724/1 "2022-10-31T14:49:42Z")

</div>

Hi,  
how can i write a thread with arguments using scripts?

---

<div class="post-metadata">

### Author: ![m.hammerton](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@m.hammerton](https://forum.universal-robots.com/u/m.hammerton)
#### Post date: [October 31, 2022, 3:00pm UTC](https://forum.universal-robots.com/t/how-to-create-a-thread-with-scripts/24724/2 "2022-10-31T15:00:31Z")

</div>

Elottrotec\_AC14,  
Can you explain a little bit more on what your trying to do?

You cant write a Thread in a script file.  
however you can call a def routine in a script file from a Thread  
or write single script commands in Polyscope within a Thread.

---

<div class="post-metadata">

### Author: ![eric.feldmann](https://avatars.discourse-cdn.com/v4/letter/e/94ad74/32.png) [@eric.feldmann](https://forum.universal-robots.com/u/eric.feldmann)
#### Post date: [October 31, 2022, 6:22pm UTC](https://forum.universal-robots.com/t/how-to-create-a-thread-with-scripts/24724/3 "2022-10-31T18:22:25Z")

</div>

You also can’t pass arguments through a thread. You will have to have the thread utilize global variables instead if you want to “pass” information into it. And you CAN write threads into a script file:

![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/8/878a5453d20d664af5fa353faa0c73f07cb81747.png)

You can read more in the script manual here: [https://s3-eu-west-1.amazonaws.com/ur-support-site/115824/scriptManual\_SW5.11.pdf](https://s3-eu-west-1.amazonaws.com/ur-support-site/115824/scriptManual_SW5.11.pdf)

Just ctrl+F for “thread”

---

<div class="post-metadata">

### Author: ![m.hammerton](https://avatars.discourse-cdn.com/v4/letter/m/b2d939/32.png) [@m.hammerton](https://forum.universal-robots.com/u/m.hammerton)
#### Post date: [November 1, 2022, 7:40am UTC](https://forum.universal-robots.com/t/how-to-create-a-thread-with-scripts/24724/4 "2022-11-01T07:40:01Z")

</div>

Thanks eric.feldmann,  
This why I love the forum there’s so much knowledge from everyday users with different experiences.

Every days a school day!

---

<div class="post-metadata">

### Author: ![terryc](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/terryc/32/3006_2.png) [@terryc](https://forum.universal-robots.com/u/terryc)
#### Post date: [November 1, 2022, 5:57pm UTC](https://forum.universal-robots.com/t/how-to-create-a-thread-with-scripts/24724/5 "2022-11-01T17:57:51Z")

</div>

No, but you can wrap a thread in a function and get around it:

You can create a function that takes arguments and inside that function, you can take those args, redeclare them with different names as global, and now your thread can use them inside that function.

If you want to see an example, try using an IF with the continuous box checked and see how UR implements this in their .script file generated when you run the URP.

Literally just did this for something 5 mins ago (Not super experienced with the whole join, kill stuff in urscript so test this)

```python

def threaded_function(a, b):
    global c = a
    global d = b

    thread my_thread():
        textmsg("value of a", a)
        textmsg("value of b", b)
    end

    my_handler = run my_thread()
    join my_handler
end

```

---

<div class="post-metadata">

### Author: ![birgit.meier](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/birgit.meier/32/4786_2.png) [@birgit.meier](https://forum.universal-robots.com/u/birgit.meier)
#### Post date: [December 19, 2022, 3:47pm UTC](https://forum.universal-robots.com/t/how-to-create-a-thread-with-scripts/24724/6 "2022-12-19T15:47:51Z")

</div>

Hello terryc,  
I’m new in writing threads, my question is, if you would have an digital input which goes on false, because the spindle is crashed… I’ll interrupt the movement of the program, with “halt”.  
Can you help with a sample or an idea how to combine both events.  
Thanks Birgit
