# Thread execution leads to error

**URL:** https://forum.universal-robots.com/t/thread-execution-leads-to-error/27540
**Category:** Technical Questions
**Created:** [March 22, 2023, 12:44pm UTC](https://forum.universal-robots.com/t/thread-execution-leads-to-error/27540 "2023-03-22T12:44:33Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![anon71786574](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@anon71786574](https://forum.universal-robots.com/u/anon71786574)
#### Post date: [March 22, 2023, 12:44pm UTC](https://forum.universal-robots.com/t/thread-execution-leads-to-error/27540/1 "2023-03-22T12:44:33Z")

</div>

Hi all,

first I watn to apologize for possibly raising an old issue again but I didn’t find an adequate (for me) solution.  
My task is rather simple: I want to stop a movej depending on the value of an digital input. I tried to do it as follows:

```auto
def test():
 
 thread moveThread():
    movej(p[-0.255, -0.624, 0.462, 1.209, -2.129, 0.759], a=1.0, v=0.05)
    movej(p[-0.52, -0.319, 0.36, 0.773, -3.071, -0.285], a=1.0, v=0.05)
    return False
 end
 
 thread stopThread():
 while(digital_in[0] < 1):
   sleep(0.02)
 end
 stopj(2)
 return False
 end
 
 moveThrd = run moveThread()
 stopThrd = run stopThread()
 join stopThrd
 kill moveThrd
 kill stopThrd
 
end

test()

```

The robot stops, as desired, but I got an error message “Another thread is already controlling the robot” (this lead me to kill both threads, but this didn’t help). Is there a better solution for this task?

Thansk in advance,

Roland

---

<div class="post-metadata">

### Author: ![anon71786574](https://avatars.discourse-cdn.com/v4/letter/a/aca169/32.png) [@anon71786574](https://forum.universal-robots.com/u/anon71786574)
#### Post date: [March 22, 2023, 2:56pm UTC](https://forum.universal-robots.com/t/thread-execution-leads-to-error/27540/2 "2023-03-22T14:56:09Z")

</div>

Hi all, thanks for reading, I solved the problem in the following way:

```auto
def test():
 
 thread moveThread():
    movej(p[-0.255, -0.624, 0.462, 1.209, -2.129, 0.759], a=1.0, v=0.05)
    movej(p[-0.52, -0.319, 0.36, 0.773, -3.071, -0.285], a=1.0, v=0.05)
	return False
 end
 
 thread allThread():
 moveThrd = run moveThread()
 while(digital_in[0] < 1):
   sleep(0.02)
 end
 kill moveThrd
 stopj(2)
 return False
 end
 
 allThrd = run allThread()
 join allThrd
end

test()

```

Cheers,

Roland
