Browse Source

Added a handy static method MessageManager::callAsync() which can asynchronously invoke a lambda on the message thread.

tags/2021-05-28
jules 11 years ago
parent
commit
df3e039bf9
2 changed files with 21 additions and 0 deletions
  1. +16
    -0
      modules/juce_events/messages/juce_MessageManager.cpp
  2. +5
    -0
      modules/juce_events/messages/juce_MessageManager.h

+ 16
- 0
modules/juce_events/messages/juce_MessageManager.cpp View File

@@ -131,6 +131,22 @@ void MessageManager::stopDispatchLoop()
#endif #endif
//============================================================================== //==============================================================================
struct AsyncFunction : private MessageManager::MessageBase
{
AsyncFunction (std::function<void(void)> f) : fn (f) { post(); }
private:
std::function<void(void)> fn;
void messageCallback() override { fn(); }
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (AsyncFunction)
};
void MessageManager::callAsync (std::function<void(void)> f)
{
new AsyncFunction (f);
}
class AsyncFunctionCallback : public MessageManager::MessageBase class AsyncFunctionCallback : public MessageManager::MessageBase
{ {
public: public:


+ 5
- 0
modules/juce_events/messages/juce_MessageManager.h View File

@@ -91,6 +91,11 @@ public:
#endif #endif
//============================================================================== //==============================================================================
/** Asynchronously invokes a function or C++11 lambda on the message thread.
Internally this uses the CallbackMessage class to invoke the callback.
*/
static void callAsync (std::function<void(void)>);
/** Calls a function using the message-thread. /** Calls a function using the message-thread.
This can be used by any thread to cause this function to be called-back This can be used by any thread to cause this function to be called-back


Loading…
Cancel
Save