# XojoScript and infinite loops

**URL:** <https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799>\
**Category:** General\
**Created:** [October 21, 2019, 6:31pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799 "2019-10-21T18:31:56Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paul\_Rodman](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_rodman/32/349_2.png) [@Paul\_Rodman](https://forum.xojo.com/u/Paul_Rodman)\
**Post date:** [October 21, 2019, 6:31pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/1 "2019-10-21T18:31:56Z")

</div>

I’m trying to detect and stop a XojoScript run if it contains an infinite loop of some kind.

I tried putting it in a secondary thread and watching its elapsed run time. However, it would seem that such a XojoScript blocks all other threads and the app just seizes up.

Any suggestions?

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 21, 2019, 6:39pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/2 "2019-10-21T18:39:33Z")

</div>

Don’t put in infinite loops? 🙂

---

<div class="post-metadata">

**Author:** ![Paul\_Rodman](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_rodman/32/349_2.png) [@Paul\_Rodman](https://forum.xojo.com/u/Paul_Rodman)\
**Post date:** [October 21, 2019, 6:41pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/3 "2019-10-21T18:41:21Z")

</div>

I won’t, but end-users are the ignorant buttheads who might. This is a web app. I don’t want them to intentionally or unintentionally bring the entire app to its knees.

---

<div class="post-metadata">

**Author:** ![Mike\_D](https://forum.xojo.com/user_avatar/forum.xojo.com/mike_d/32/266_2.png) [@Mike\_D](https://forum.xojo.com/u/Mike_D)\
**Post date:** [October 21, 2019, 6:46pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/4 "2019-10-21T18:46:12Z")

</div>

Are you letting users enter their own xojoscript? if so, a general solution to the problem is impossible, and is a major theorem of computer science: [https://en.wikipedia.org/wiki/Halting\_problem](https://en.wikipedia.org/wiki/Halting_problem)

As a practical solution, you could probably spin off a second executable to run the xojoscript and then kill it if it’s running for too long. But you can’t do that in the same process, I believe.

---

<div class="post-metadata">

**Author:** ![Paul\_Rodman](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_rodman/32/349_2.png) [@Paul\_Rodman](https://forum.xojo.com/u/Paul_Rodman)\
**Post date:** [October 21, 2019, 6:48pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/5 "2019-10-21T18:48:53Z")

</div>

It’s just a pity XojoScript.run doesn’t yield to other threads.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 21, 2019, 6:49pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/6 "2019-10-21T18:49:02Z")

</div>

Michael beat me to it. 🙂

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 21, 2019, 7:15pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/7 "2019-10-21T19:15:39Z")

</div>

Just chatted with @Paul Lefebvre and he offered something that might do it. Have the script run in a Context module with a YieldToNextThread method that calls App.YieldToNextThread.

Here’s my addition: when your users post a script, intervene and insert that call before any line that starts with Next, Wend or Loop.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 21, 2019, 7:19pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/8 "2019-10-21T19:19:12Z")

</div>

Here’s a RegEx for it:

```auto
rx.SearchPattern = "^[\\x20\\t]*(Next|Loop|Wend)\\b"
rx.ReplacementPattern = "YieldToNextThread\
\\0"
```

Edit: Tweaked the pattern a bit.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 21, 2019, 7:21pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/9 "2019-10-21T19:21:31Z")

</div>

If you prefer, the module can keep a counter so it only actually calls App.YieldToNextThread every x times it’s called.

Edit: Or every m ticks (or milliseconds).

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 21, 2019, 7:32pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/10 "2019-10-21T19:32:14Z")

</div>

I keep saying “Module”, but of course, it’s a Class.

Anyway, I just tested this and it works perfectly.

---

<div class="post-metadata">

**Author:** ![Paul\_Rodman](https://forum.xojo.com/user_avatar/forum.xojo.com/paul_rodman/32/349_2.png) [@Paul\_Rodman](https://forum.xojo.com/u/Paul_Rodman)\
**Post date:** [October 21, 2019, 7:53pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/11 "2019-10-21T19:53:11Z")

</div>

Excellent. Thanks. Should prolly add “goto” to that list for evil people who don’t know any better and use backwards gotos in their code.

---

<div class="post-metadata">

**Author:** ![Kem\_Tekinay](https://forum.xojo.com/user_avatar/forum.xojo.com/kem_tekinay/32/154_2.png) [@Kem\_Tekinay](https://forum.xojo.com/u/Kem_Tekinay)\
**Post date:** [October 21, 2019, 7:57pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/12 "2019-10-21T19:57:02Z")

</div>

Or reject any script that contains a line that starts with Goto, and, for good measure, disables or outright deletes that user’s account, blocks them from any further access to your app, web site, or company, then sends them a nasty letter (postage-due, of course).

---

<div class="post-metadata">

**Author:** ![Norman\_Palardy](https://forum.xojo.com/user_avatar/forum.xojo.com/norman_palardy/32/169_2.png) [@Norman\_Palardy](https://forum.xojo.com/u/Norman_Palardy)\
**Post date:** [October 21, 2019, 11:13pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/14 "2019-10-21T23:13:29Z")

</div>

goto has uses … few but they exist  
otherwise you write really contorted code to avoid them  
there were still one or two in the IDE last I looked

---

<div class="post-metadata">

**Author:** ![Robert\_Birge](https://forum.xojo.com/user_avatar/forum.xojo.com/robert_birge/32/210_2.png) [@Robert\_Birge](https://forum.xojo.com/u/Robert_Birge)\
**Post date:** [October 22, 2019, 12:32am UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/15 "2019-10-22T00:32:27Z")

</div>

[quote=459565:@Paul Rodman]Paul Rodman 6 hours ago Pre-Release Testers, Xojo Pro Kirkland, WA  
It’s just a pity XojoScript.run doesn’t yield to other threads.[/quote]

But I think it does yield to timer calls. I have used that in the past to update the interface during long XojoScripts.

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [October 22, 2019, 12:38am UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/16 "2019-10-22T00:38:46Z")

</div>

> [@459599:@Robert Birge](#):
>
> But I think it does yield to timer calls. I have used that in the past to update the interface during long XojoScripts.

Then a timer could be used to kill the XojoScript after a given delay. But the idea to launch a new copy of the app exclusively for XojoScript is probably the best, to avoid the main app lock.

---

<div class="post-metadata">

**Author:** ![Robert\_Birge](https://forum.xojo.com/user_avatar/forum.xojo.com/robert_birge/32/210_2.png) [@Robert\_Birge](https://forum.xojo.com/u/Robert_Birge)\
**Post date:** [October 22, 2019, 2:24am UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/17 "2019-10-22T02:24:28Z")

</div>

> [@459600:@Michel Bujardet](#):
>
> But the idea to launch a new copy of the app exclusively for XojoScript is probably the best, to avoid the main app lock.

Are you proposing to use a shell here, or to launch a thread containing the XojoScript?

---

<div class="post-metadata">

**Author:** ![Michel\_Bujardet](https://forum.xojo.com/user_avatar/forum.xojo.com/michel_bujardet/32/252_2.png) [@Michel\_Bujardet](https://forum.xojo.com/u/Michel_Bujardet)\
**Post date:** [October 22, 2019, 8:42am UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/18 "2019-10-22T08:42:53Z")

</div>

You could have an app dedicated to XojoScript, which would be launched from a FolderItem.Launch in API 1 or a shell in API 2, and provide the name of the file containing the XojoScript source. The user is presented with that app’s screen from then on.

That app can have a TimeOut timer to kill itself after a preset period, to avoid infinite loops, which would return the user to the calling app, and then quit.

---

<div class="post-metadata">

**Author:** ![system](https://forum.xojo.com/uploads/default/original/1X/455b4dcc0e61630e9f81940004ddffa49c432a46.png) [@system](https://forum.xojo.com/u/system)\
**Post date:** [April 29, 2021, 4:08pm UTC](https://forum.xojo.com/t/xojoscript-and-infinite-loops/50799/19 "2021-04-29T16:08:17Z")

</div>


