# Event vs. Thread

**URL:** https://forum.universal-robots.com/t/event-vs-thread/19100
**Category:** Technical Questions
**Created:** [February 7, 2022, 8:20pm UTC](https://forum.universal-robots.com/t/event-vs-thread/19100 "2022-02-07T20:20:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![anon41178160](https://avatars.discourse-cdn.com/v4/letter/a/ecccb3/32.png) [@anon41178160](https://forum.universal-robots.com/u/anon41178160)
#### Post date: [February 7, 2022, 8:20pm UTC](https://forum.universal-robots.com/t/event-vs-thread/19100/1 "2022-02-07T20:20:24Z")

</div>

Hello all,

In perusing through the UR and Polyscope manuals, I am having a hard time telling the difference between the efficiency and use of the “Thread” command, and the “Event” command. Is “Thread” used to contain a number of various commands as a way of organization, whilst “Event” is reserved for simpler tasks?

Any help would be appreciated, thanks

---

<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: [February 7, 2022, 9:01pm UTC](https://forum.universal-robots.com/t/event-vs-thread/19100/2 "2022-02-07T21:01:46Z")

</div>

“Threads” in programming are parallel tasks. They run AT THE SAME TIME as your main program. This can be useful for monitoring inputs or other sensor values. Think of this as another Main Program.

Events are exactly the same thing as a thread, it’s just that Polyscope does a little more house keeping for you. In effect, it will run the code contained in the event ONCE per desired input condition. In fact, the script code Polyscope generates is just a thread:

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

In short, an event does nothing that a thread cannot do, it just does a little bit of additional prep work for you if you want code to run ONCE as opposed to continuously

---

<div class="post-metadata">

### Author: ![efn](https://avatars.discourse-cdn.com/v4/letter/e/35a633/32.png) [@efn](https://forum.universal-robots.com/u/efn)
#### Post date: [February 8, 2022, 6:10am UTC](https://forum.universal-robots.com/t/event-vs-thread/19100/3 "2022-02-08T06:10:18Z")

</div>

It is a little misleading saying it will run ONCE, isn’t it? 🙂 It’s a while-command and will act entirely as a thread and loop itself continuously as long as the input is true.

If you want to only run the event once, the event input must be set false before the execution of the last command (or as the last command) of the event.

---

<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: [February 8, 2022, 12:48pm UTC](https://forum.universal-robots.com/t/event-vs-thread/19100/4 "2022-02-08T12:48:11Z")

</div>

Oops, yep you’re 100% correct!

---

<div class="post-metadata">

### Author: ![bba](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.universal-robots.com/bba/32/555_2.png) [@bba](https://forum.universal-robots.com/u/bba)
#### Post date: [February 9, 2022, 6:15pm UTC](https://forum.universal-robots.com/t/event-vs-thread/19100/5 "2022-02-09T18:15:10Z")

</div>

The other thing to be careful about with events is that IF you have multiple events configured and event A is configured to execute first (within polyscope) and event B is second, IF and while event A is active only it will execute (out of all the events) and event B will not execute. If event B is active (while event A is not) and then event A becomes active the code under event A will execute and event B will not.

I agree this is not exactly intuitive but it is the way it works.

Now a straight thread(s) that is/are configured WILL run regardless of which event is active. Events do not affect threads but they can affect other events.

---

<div class="post-metadata">

### Author: ![dpeva](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@dpeva](https://forum.universal-robots.com/u/dpeva)
#### Post date: [October 4, 2022, 9:19pm UTC](https://forum.universal-robots.com/t/event-vs-thread/19100/6 "2022-10-04T21:19:42Z")

</div>

Can anyone explain why this event works but what appears to be an equivalent thread doesn’t?

![image](https://us1.discourse-cdn.com/flex020/uploads/universal_robots/original/2X/0/03875cf3f05e4009e603a6676ca4dc861d838744.png)

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

Background:  
robot is loading a part onto a gage, which holds the input named Part\_accept (analog\_in(1)) at +5V unless the part fails, where it drops to zero.

**The Event** (one of several events in the program) shows up in the program’s script file as:

elif (get\_standard\_analog\_in(1) \< 2.0):

```
    global part_fail= True  

```

**The Thread shows up as:**

thread Thread\_1():

```
if (get_standard_analog_in(1)<2.0):

  global part_fail= True  

end

```

If I suppress the Event and use the thread, it completely ignores the part failure.  
If I suppress the thread and use the Event, it works as expected.

I’ve tried sticking a sync() command into the thread, with no effect.

Just confuses me that the thread doesn’t seem to work.
