# Problem with the lack of a simple Delay() or Sleep() method

**URL:** <https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543>\
**Category:** General\
**Created:** [October 5, 2023, 11:18am UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543 "2023-10-05T11:18:46Z")\
**Posts on this page:** 20\
**Page:** 2

<div class="post-metadata">

**Author:** ![Sascha\_S](https://forum.xojo.com/user_avatar/forum.xojo.com/sascha_s/32/22328_2.png) [@Sascha\_S](https://forum.xojo.com/u/Sascha_S)\
**Post date:** [October 5, 2023, 5:43pm UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/21 "2023-10-05T17:43:38Z")

</div>

True in most cases. But i meant pauses in general, not a specific Pause instruction. 😊

---

<div class="post-metadata">

**Author:** ![Chris\_Mower](https://forum.xojo.com/letter_avatar_proxy/v4/letter/c/e99b99/32.png) [@Chris\_Mower](https://forum.xojo.com/u/Chris_Mower)\
**Post date:** [October 5, 2023, 6:08pm UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/22 "2023-10-05T18:08:58Z")

</div>

I cut my teeth on a computer system that used the wonderfully named Panic assembly language. It was part of the air defence system at Staxton Wold, and rather like the old PDP-8 you had to toggle in the boot loader to load a paper tape to enable the teletype to start writing code. To quote the Four Yorkshiremen sketch “You tell the young ones today and they won’t believe you!” 🤣 🤣

---

<div class="post-metadata">

**Author:** ![Jonathan\_Ashwell](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/c89c15/32.png) [@Jonathan\_Ashwell](https://forum.xojo.com/u/Jonathan_Ashwell)\
**Post date:** [October 5, 2023, 7:05pm UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/23 "2023-10-05T19:05:38Z")

</div>

I don’t know if this is exactly what you’re after, or if you have or want to have the MBS plugins, but there is a DelayMBS() method.

[https://www.monkeybreadsoftware.net/global-delaymbs.shtml](https://www.monkeybreadsoftware.net/global-delaymbs.shtml)

---

<div class="post-metadata">

**Author:** ![Matthew\_Stevens](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Matthew\_Stevens](https://forum.xojo.com/u/Matthew_Stevens)\
**Post date:** [October 7, 2023, 10:05am UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/24 "2023-10-07T10:05:35Z")

</div>

> [@Christian\_Wheel](#):
>
> This forum is an interesting place. It has grown on me over the years, but one thing that takes a little getting used to is needing to justify one’s approach to the crowd. I have come to discover that it comes from a place of caring.

That is a very diplomatic way of putting it. Well done.

> [@](#):
>
> There are always those of us who have fringe cases though, like allowing time for a hardware port to settle.

To my mind asynchronous problems need asynchronous solutions and synchronous problems need synchronous solutions. (Even if advocates of one highly abstracted programming language do not recognise it.)

A 10ms pause is 1/4 of a frame at 25Hz. Why would anyone be concerned about locking the GUI?

```auto
function ShortWait(millis as integer)
  var now as integer
  var start as integer = system.microseconds / 1000
  do
    now = system.microseconds / 1000
  loop until now - start > millis
end function
```

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [October 7, 2023, 10:13am UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/25 "2023-10-07T10:13:50Z")

</div>

Why loop the CPU, especially given that `thread.SleepCurrent()` is available.

---

<div class="post-metadata">

**Author:** ![Matthew\_Stevens](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Matthew\_Stevens](https://forum.xojo.com/u/Matthew_Stevens)\
**Post date:** [October 7, 2023, 10:37am UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/26 "2023-10-07T10:37:58Z")

</div>

Why not?

Let me put that another way. What is the tangible benefit of thread.sleepCurrent in this use case. Not trying to say there is not one.

---

<div class="post-metadata">

**Author:** ![Ian\_Kennedy](https://forum.xojo.com/user_avatar/forum.xojo.com/ian_kennedy/32/6860_2.png) [@Ian\_Kennedy](https://forum.xojo.com/u/Ian_Kennedy)\
**Post date:** [October 7, 2023, 11:24am UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/27 "2023-10-07T11:24:39Z")

</div>

Energy consumption, battery drain, thermal effects. Many good reasons.

---

<div class="post-metadata">

**Author:** ![Matthew\_Stevens](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Matthew\_Stevens](https://forum.xojo.com/u/Matthew_Stevens)\
**Post date:** [October 7, 2023, 1:59pm UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/28 "2023-10-07T13:59:15Z")

</div>

I like that you found reasons. The delay being tens of milliseconds and the hysteresis of the things you mention being nearer to minutes. I would argue they are not especially good reasons.

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [October 7, 2023, 4:22pm UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/29 "2023-10-07T16:22:18Z")

</div>

> [@Matthew\_Stevens](#):
>
> What is the tangible benefit of thread.sleepCurrent in this use case.

One line of code instead of five?

---

<div class="post-metadata">

**Author:** ![Eric\_Williams](https://forum.xojo.com/letter_avatar_proxy/v4/letter/e/ecccb3/32.png) [@Eric\_Williams](https://forum.xojo.com/u/Eric_Williams)\
**Post date:** [October 7, 2023, 4:22pm UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/30 "2023-10-07T16:22:51Z")

</div>

Your users may disagree. Any app that periodically consumed 100% of a CPU while apparently doing nothing is automatically suspicious.

Besides - even if it only saves a little energy - why not? Isn’t that worth it for such a tiny code change?

---

<div class="post-metadata">

**Author:** ![Matthew\_Stevens](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Matthew\_Stevens](https://forum.xojo.com/u/Matthew_Stevens)\
**Post date:** [October 7, 2023, 4:54pm UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/31 "2023-10-07T16:54:20Z")

</div>

> [@Eric\_Williams](#):
>
> Your users may disagree. Any app that periodically consumed 100% of a CPU while apparently doing nothing is automatically suspicious.

Needing a synchronous delay of 100ms or less pretty much discounts humans being involved in that particular interaction. I attempted to capture the 100% CPU use with Resource Monitor but needed a delay of several seconds before it registered at all.

> [@](#):
>
> Besides - even if it only saves a little energy - why not? Isn’t that worth it for such a tiny code change?

I don’t accept it saves any energy. I think at 100ms or less the difference in energy consumption is going to be indistinguishable amongst the noise floor.

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [October 7, 2023, 7:05pm UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/32 "2023-10-07T19:05:54Z")

</div>

fault tolerance. You may not be the one to abuse such feature and run in a loop for example, but anotherone may do that and have lot’s of issues. If one puts your requested answer in the first post, then many people (newbies mostly) will have issues caused by the answer on the forums.

I believe you have enough answers to your question by now.

---

<div class="post-metadata">

**Author:** ![brian\_franco](https://forum.xojo.com/user_avatar/forum.xojo.com/brian_franco/32/156_2.png) [@brian\_franco](https://forum.xojo.com/u/brian_franco)\
**Post date:** [October 7, 2023, 7:15pm UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/33 "2023-10-07T19:15:38Z")

</div>

Or mark it for advanced users?  
I’m not saying anything is wrong or right but if you focus on impact of new users then I see no reason to hide this discussion just maybe mark as advanced/not recommended ?

What if someone was looking exactly for what @Matthew_Stevens described?

---

<div class="post-metadata">

**Author:** ![Matthew\_Stevens](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Matthew\_Stevens](https://forum.xojo.com/u/Matthew_Stevens)\
**Post date:** [October 8, 2023, 8:27am UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/34 "2023-10-08T08:27:32Z")

</div>

> [@TimStreater](#):
>
> Why loop the CPU, especially given that `thread.SleepCurrent()` is available.

@TimStreater My only reason for using a loop is familiarity - I know it works. As I mentioned, I’m not going to fret about blocking for a 100ms or less so had no reason to look for an event driven alternative. I am open to there being a better way but would like to hear how it is better before refactoring.

Turns out there is a good reason to use a CPU loop. thread.SleepCurrent() simply isn’t very accurate in the tens, or even hundreds, of milliseconds range. Returning before the requested delay has expired is strictly ‘verboten’ in many machine comms protocols and a potential source of difficult to isolate bugs when it happens.

```auto
Microseconds
Sleep Wait
96,574	100,001
96,834	100,001
101,231	100,001
108,381	100,001
112,529	100,001
109,577	100,001
110,638	100,001

```

```auto
//button1.action
const DELAY_MILLIS as int64 = 100

var now as int64, start As int64, stop as int64
var resultSleep as string, resultWait as string

//sleep
start = System.Microseconds
Thread.SleepCurrent(DELAY_MILLIS)
resultSleep = Format(system.Microseconds - start, "###,###")

//wait
start = System.Microseconds
stop = start + (DELAY_MILLIS * 1000)
do
  now = system.Microseconds
loop until now > stop
resultWait = Format(system.Microseconds - start, "###,###")

var results() as string = array(resultSleep, resultWait)
ListBox1.AddRow(results)
```

---

<div class="post-metadata">

**Author:** ![DerkJ](https://forum.xojo.com/letter_avatar_proxy/v4/letter/d/e495f1/32.png) [@DerkJ](https://forum.xojo.com/u/DerkJ)\
**Post date:** [October 8, 2023, 8:40am UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/35 "2023-10-08T08:40:04Z")

</div>

Well Xojo still isn’t designed that way. Don’t use App.DoEvents unless you know what you are doing it’s not actually delaying only! Sp not meant for the OP s question.

---

<div class="post-metadata">

**Author:** ![TimStreater](https://forum.xojo.com/user_avatar/forum.xojo.com/timstreater/32/586_2.png) [@TimStreater](https://forum.xojo.com/u/TimStreater)\
**Post date:** [October 8, 2023, 8:59am UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/36 "2023-10-08T08:59:26Z")

</div>

> [@Matthew\_Stevens](#):
>
> Turns out there is a good reason to use a CPU loop. thread.SleepCurrent() simply isn’t very accurate in the tens, or even hundreds, of milliseconds range. Returning before the requested delay has expired is strictly ‘verboten’ in many machine comms protocols and a potential source of difficult to isolate bugs when it happens.

Are you running Windows? And your CPU loop delay will need to be tuned for your processor freqeuncy, and so mayl change on another CPU.

---

<div class="post-metadata">

**Author:** ![Matthew\_Stevens](https://forum.xojo.com/letter_avatar_proxy/v4/letter/m/c4cdca/32.png) [@Matthew\_Stevens](https://forum.xojo.com/u/Matthew_Stevens)\
**Post date:** [October 8, 2023, 9:24am UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/37 "2023-10-08T09:24:29Z")

</div>

> [@TimStreater](#):
>
> Are you running Windows? And your CPU loop delay will need to be tuned for your processor freqeuncy, and so mayl change on another CPU.

I have tried it on Windows and Mac. thread.sleepcurrent is better on a Mac but still no where near as accurate as the loop, and still sometimes returns before the requested delay time has expired.

Why would I need to tune the loop? A microsecond on one CPU is much the same as any other.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [October 8, 2023, 11:42am UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/38 "2023-10-08T11:42:28Z")

</div>

Who would have thought that a question like “how do I do a sleep(100) in Xojo?” would yield almost 40 posts filled with so much confusion?

Please, just mark the post [Problem with the lack of a simple Delay() or Sleep() method - #4 by TimStreater](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/4) as the answer and let’s move on.

---

<div class="post-metadata">

**Author:** ![Rick\_Araujo](https://forum.xojo.com/user_avatar/forum.xojo.com/rick_araujo/32/251_2.png) [@Rick\_Araujo](https://forum.xojo.com/u/Rick_Araujo)\
**Post date:** [October 8, 2023, 11:50am UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/39 "2023-10-08T11:50:32Z")

</div>

> [@Matthew\_Stevens](#):
>
> I have tried it on Windows and Mac. thread.sleepcurrent is better on a Mac but still no where near as accurate as the loop, and still sometimes returns before the requested delay time has expired.

If you think you’ve found a bug, report the issue with the test sample, please: [https://xojo.com/issues](https://xojo.com/issues)

---

<div class="post-metadata">

**Author:** ![Julia\_Truchsess](https://forum.xojo.com/user_avatar/forum.xojo.com/julia_truchsess/32/499_2.png) [@Julia\_Truchsess](https://forum.xojo.com/u/Julia_Truchsess)\
**Post date:** [October 8, 2023, 2:11pm UTC](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/40 "2023-10-08T14:11:48Z")

</div>

> [@Rick\_Araujo](#):
>
> Please, just mark the post [Problem with the lack of a simple Delay() or Sleep() method - #4 by TimStreater](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543/4) as the answer and let’s move on.

What’s wrong with having a discussion? Is there a forum storage limitation? Thanks to @Matthew_Stevens we now have quantification of variations in thread.sleep timing that we wouldn’t have if we’d “moved on”. Information is good.

[Previous page](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543.md?page=1)

[Next page](https://forum.xojo.com/t/problem-with-the-lack-of-a-simple-delay-or-sleep-method/77543.md?page=3)
