# Updating GUI from other threads

**URL:** <https://forum.juce.com/t/updating-gui-from-other-threads/20906>\
**Category:** General JUCE discussion\
**Created:** [February 10, 2017, 12:42pm UTC](https://forum.juce.com/t/updating-gui-from-other-threads/20906 "2017-02-10T12:42:12Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 10, 2017, 12:42pm UTC](https://forum.juce.com/t/updating-gui-from-other-threads/20906/1 "2017-02-10T12:42:13Z")

</div>

Hi there, what’s the preferred way of handling UI updates from other threads that doesn’t involve creating MessageManagerLock instances. Is there a general pattern for handling this scenario?

thx

---

<div class="post-metadata">

**Author:** ![martinrobinson-2](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/martinrobinson-2/32/630_2.png) [@martinrobinson-2](https://forum.juce.com/u/martinrobinson-2)\
**Post date:** [February 10, 2017, 12:52pm UTC](https://forum.juce.com/t/updating-gui-from-other-threads/20906/2 "2017-02-10T12:52:00Z")

</div>

It depends on what you’re doing and whether the “other” thread is the audio thread. A simple mechanism for non-audio \> main thread communication for components is using `postCommandMessage()` and `handleCommandMessage()`. If the other thread is the audio thread then don’t do that (as `postCommandMessage()` calls `new` and in any case you might risk flooding the message queue). In this case using a `Timer` to check a flag (which you can set in your audio thread) works well.

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 10, 2017, 12:55pm UTC](https://forum.juce.com/t/updating-gui-from-other-threads/20906/3 "2017-02-10T12:55:52Z")

</div>

thanks for reply - the other thread in this case is a MIDI thread, so I’ll look into postCommandMessage - cheers!

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [February 26, 2017, 5:58pm UTC](https://forum.juce.com/t/updating-gui-from-other-threads/20906/4 "2017-02-26T17:58:27Z")

</div>

at what point is it valid to call postCommandMessage() and have the message delivered? messages that I send early on never get delivered…

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [February 26, 2017, 7:27pm UTC](https://forum.juce.com/t/updating-gui-from-other-threads/20906/5 "2017-02-26T19:27:02Z")

</div>

Try using AsyncUpdater… using a Message can overload the Message queue

Simply triggerAsyncUpdate() and in handleAsyncUpdate() do a repaint().

Rail

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [March 2, 2017, 11:20am UTC](https://forum.juce.com/t/updating-gui-from-other-threads/20906/6 "2017-03-02T11:20:04Z")

</div>

thx this is much better. Shame it doesn’t have a message to pass, but that’s easy to implement… thx for the suggestion

---

<div class="post-metadata">

**Author:** ![jimc](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/jimc/32/16067_2.png) [@jimc](https://forum.juce.com/u/jimc)\
**Post date:** [March 2, 2017, 11:21am UTC](https://forum.juce.com/t/updating-gui-from-other-threads/20906/7 "2017-03-02T11:21:50Z")

</div>

Someone should write a book on this topic 🙂

MessageManager::callAsync() is pretty good - but you need to faff around with weak pointers if there’s any danger of the target object being deleted while the message is in the queue.

I tend to use this a lot:

> <https://github.com/jcredland/jcf_lime_juce/blob/master/utils/lock_free_call_queue.h>

---

<div class="post-metadata">

**Author:** ![yfede](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/yfede/32/506_2.png) [@yfede](https://forum.juce.com/u/yfede)\
**Post date:** [March 2, 2017, 11:52am UTC](https://forum.juce.com/t/updating-gui-from-other-threads/20906/8 "2017-03-02T11:52:06Z")

</div>

> [@leehu](#):
>
> Shame it doesn’t have a message to pass

ActionBroadcaster / ActionListener does that with a String object, if that carries enough information for you

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [March 2, 2017, 11:52am UTC](https://forum.juce.com/t/updating-gui-from-other-threads/20906/9 "2017-03-02T11:52:47Z")

</div>

great - thx for the info, that’ll do nicely in this case

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [March 2, 2017, 7:26pm UTC](https://forum.juce.com/t/updating-gui-from-other-threads/20906/10 "2017-03-02T19:26:00Z")

</div>

I _think_ (and you can check me on this) ActionBroadcaster has a lock while ASyncUpdater doesn’t (which may or may nor matter depending on what you’re doing).

Cheers,

Rail
