# Thread stops without warning

**URL:** <https://forum.universal-robots.com/t/thread-stops-without-warning/28382>\
**Category:** URScript\
**Created:** [May 12, 2023, 7:18am UTC](https://forum.universal-robots.com/t/thread-stops-without-warning/28382 "2023-05-12T07:18:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![florian.gaillard](https://avatars.discourse-cdn.com/v4/letter/f/b5a626/32.png) [@florian.gaillard](https://forum.universal-robots.com/u/florian.gaillard)\
**Post date:** [May 12, 2023, 7:18am UTC](https://forum.universal-robots.com/t/thread-stops-without-warning/28382/1 "2023-05-12T07:18:21Z")

</div>

Hello,  
I use a URScript thread to make a light blink:

> thread vert\_clignotant\_gauche\_thread():  
> vert\_clignotant\_gauche\_polarity=False  
> while True:  
> if vert\_clignotant\_gauche\_polarity:  
> set\_analog\_out(0, 0.5)  
> vert\_clignotant\_gauche\_polarity=False  
> else:  
> set\_analog\_out(0, 0)  
> vert\_clignotant\_gauche\_polarity=True  
> end  
> sleep(0.5)  
> end  
> end

When the robot is not moving, it works fine.  
However, when the robot is moving, the thread gets launched, it does one loop in the while and then stops without showing any warning in the logs.

Any idea what is happening?

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:** [May 12, 2023, 12:46pm UTC](https://forum.universal-robots.com/t/thread-stops-without-warning/28382/2 "2023-05-12T12:46:20Z")

</div>

Syntax seems good. I usually include a sync() command inside most while(true) loops. I’m guessing the only reason you don’t see a runtime error is due to the sleep command, but it happening only when the robot is moving makes it sound like a possible scheduling error, which a sync() might correct.

---

<div class="post-metadata">

**Author:** ![florian.gaillard](https://avatars.discourse-cdn.com/v4/letter/f/b5a626/32.png) [@florian.gaillard](https://forum.universal-robots.com/u/florian.gaillard)\
**Post date:** [May 12, 2023, 1:13pm UTC](https://forum.universal-robots.com/t/thread-stops-without-warning/28382/3 "2023-05-12T13:13:47Z")

</div>

Thanks for your reply. Would you place the sync() before or after the sleep command?

---

<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:** [May 12, 2023, 1:52pm UTC](https://forum.universal-robots.com/t/thread-stops-without-warning/28382/4 "2023-05-12T13:52:58Z")

</div>

I usually put it as the last instruction before the loop restarts, so right after the sleep. I’d probably throw another sync() before the End of the actual thread as well, just for kicks.

---

<div class="post-metadata">

**Author:** ![florian.gaillard](https://avatars.discourse-cdn.com/v4/letter/f/b5a626/32.png) [@florian.gaillard](https://forum.universal-robots.com/u/florian.gaillard)\
**Post date:** [May 16, 2023, 12:47pm UTC](https://forum.universal-robots.com/t/thread-stops-without-warning/28382/5 "2023-05-16T12:47:11Z")

</div>

Hello, I just tried what you mentionned, the behaviour did not change. Any idea?  
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:** [May 16, 2023, 2:22pm UTC](https://forum.universal-robots.com/t/thread-stops-without-warning/28382/6 "2023-05-16T14:22:36Z")

</div>

The script you sent shows the definition of the thread. When are you actually starting it?

---

<div class="post-metadata">

**Author:** ![florian.gaillard](https://avatars.discourse-cdn.com/v4/letter/f/b5a626/32.png) [@florian.gaillard](https://forum.universal-robots.com/u/florian.gaillard)\
**Post date:** [May 16, 2023, 3:46pm UTC](https://forum.universal-robots.com/t/thread-stops-without-warning/28382/7 "2023-05-16T15:46:17Z")

</div>

I launch a thread in an installation URCAP what is constantly watching a push button.  
If the button is pushed, then it launches light blink thread.  
The button thread does not stop because I still can see global variables changing when I push the button

---

<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:** [May 16, 2023, 7:02pm UTC](https://forum.universal-robots.com/t/thread-stops-without-warning/28382/8 "2023-05-16T19:02:01Z")

</div>

How are you monitoring it/launching it from a button push in the CAP? If you’re using sockets, you may be running it as primary when it should use the secondary socket. I usually just launch all my threads to start, and include a locking boolean that I control in the code. So like:

```auto
while(runThread == False):
  sync()
end
//Do
//Thread
//Stuff

```

That way the thread is always running, and unlocks itself when the variable changes to true. Not sure if that could fix your problem or not

---

<div class="post-metadata">

**Author:** ![florian.gaillard](https://avatars.discourse-cdn.com/v4/letter/f/b5a626/32.png) [@florian.gaillard](https://forum.universal-robots.com/u/florian.gaillard)\
**Post date:** [May 24, 2023, 2:26pm UTC](https://forum.universal-robots.com/t/thread-stops-without-warning/28382/9 "2023-05-24T14:26:02Z")

</div>

Your solution made it work. Thanks a lot.
