# thread sleep, suspend, resume

**URL:** <https://forum.xojo.com/t/thread-sleep-suspend-resume/28385>\
**Category:** General\
**Created:** [November 30, 2015, 7:31pm UTC](https://forum.xojo.com/t/thread-sleep-suspend-resume/28385 "2015-11-30T19:31:55Z")\
**Posts on this page:** 9\
**Page:** 1

<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:** [November 30, 2015, 7:31pm UTC](https://forum.xojo.com/t/thread-sleep-suspend-resume/28385/1 "2015-11-30T19:31:55Z")

</div>

If my thread suspends itself (using `thread.suspend()` ), then it appears that the thread can be resumed (using `thread.resume()` ) from an event handler. So far so good. However I’d thought to use `sleep()` instead so that I could effect a timeout in a simple manner - I want to wait for the event but not forever. I tried `thread.sleep(someperiod, true)` as the documentation for `sleep()` indicates, but the thread always waits for the full period instead of being resumed when the event occurs.

Have I overlooked something here or is there an issue with `sleep()` ? I can probably do the timeout using a separate timer but it would be nice to avoid having to do that.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [November 30, 2015, 8:37pm UTC](https://forum.xojo.com/t/thread-sleep-suspend-resume/28385/2 "2015-11-30T20:37:16Z")

</div>

[quote=Xojo docs]It will wake up early if there are no other threads able to execute.  
[/quote]  
The key phrase is “able to execute”. That means blocked, not idle. If the main thread is simply idle, your thread will sleep the full time.

---

<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:** [November 30, 2015, 8:56pm UTC](https://forum.xojo.com/t/thread-sleep-suspend-resume/28385/3 "2015-11-30T20:56:36Z")

</div>

Humphh! Well, that’s no fun. Is that for a reason, or possibly the consequence of some architectural feature of Xojo? The ability for sleep to return early would appear on the face of it to be not too useful if it cannot be relied upon.

---

<div class="post-metadata">

**Author:** ![Tim\_Hare](https://forum.xojo.com/user_avatar/forum.xojo.com/tim_hare/32/176_2.png) [@Tim\_Hare](https://forum.xojo.com/u/Tim_Hare)\
**Post date:** [November 30, 2015, 9:10pm UTC](https://forum.xojo.com/t/thread-sleep-suspend-resume/28385/4 "2015-11-30T21:10:03Z")

</div>

Sleep for shorter periods in a loop until you event occurs. An alternative would be to have the thread register with the event somehow and have the event notify everyone who is watching. (Observer Pattern)

---

<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:** [November 30, 2015, 9:24pm UTC](https://forum.xojo.com/t/thread-sleep-suspend-resume/28385/5 "2015-11-30T21:24:51Z")

</div>

What I’ve actually done is resume the thread from the event handler. If the event never occurs, the thread gets resumed from a timer I’ve added (the one I was hoping to avoid adding by using `thread.sleep(period, true)` ). I keep a pointer to the thread in the socket (which I’ve subclassed) and also the timer I just mentioned (also subclassed).

I’m starting to get the hang of how to do this in Xojo now. The principles I’m familiar with, having worked on the innards of thread libraries for some apps under VMS and VM/CMS in the 80s.

I also noticed something interesting about the task class. If you instantiate the class, instead of adding it permanently as a control, then when the thread terminates it goes out of scope. This means that the last modification to the UI risks not being done as the associated timer is killed off. I added a sleep for 100 msec or so at the end of the thread code to solve that.

---

<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:** [December 1, 2015, 3:28pm UTC](https://forum.xojo.com/t/thread-sleep-suspend-resume/28385/6 "2015-12-01T15:28:49Z")

</div>

> [@233012:@Tim Streater](#):
>
> I also noticed something interesting about the task class. If you instantiate the class, instead of adding it permanently as a control, then when the thread terminates it goes out of scope. This means that the last modification to the UI risks not being done as the associated timer is killed off. I added a sleep for 100 msec or so at the end of the thread code to solve that.

Another thing to worry about is if the control goes out of scope (e.g. due to the window closing) while the thread / timer is still active : this can give a crash.

If you haven’t yet, check out Weak References [http://documentation.xojo.com/index.php/WeakRef](http://documentation.xojo.com/index.php/WeakRef) which are a handy way of solving that problem.

---

<div class="post-metadata">

**Author:** ![Joe\_Ranieri](https://forum.xojo.com/letter_avatar_proxy/v4/letter/j/43a26b/32.png) [@Joe\_Ranieri](https://forum.xojo.com/u/Joe_Ranieri)\
**Post date:** [December 2, 2015, 5:50pm UTC](https://forum.xojo.com/t/thread-sleep-suspend-resume/28385/7 "2015-12-02T17:50:28Z")

</div>

> [@233012:@Tim Streater](#):
>
> I also noticed something interesting about the task class. If you instantiate the class, instead of adding it permanently as a control, then when the thread terminates it goes out of scope. This means that the last modification to the UI risks not being done as the associated timer is killed off. I added a sleep for 100 msec or so at the end of the thread code to solve that.

Given that this is an example project we ship, could you file a bug report?

---

<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:** [December 8, 2015, 12:09pm UTC](https://forum.xojo.com/t/thread-sleep-suspend-resume/28385/8 "2015-12-08T12:09:45Z")

</div>

OK - done that.

---

<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:** [October 29, 2020, 6:04pm UTC](https://forum.xojo.com/t/thread-sleep-suspend-resume/28385/9 "2020-10-29T18:04:58Z")

</div>


