The MessageThread class implements a thread with a ready to use MessageQueue and MessageDispatch class.
More...
#include "murl_util_message_thread.h"
The MessageThread class implements a thread with a ready to use MessageQueue and MessageDispatch class.
Example for sending data to different methods:
#include "murl_util_message_thread.h"
class MyMessage1 : public Util::Message
{
public:
: Util::Message(messageId)
, mData(data)
{
}
};
class MyMessage2 : public Util::Message
{
public:
: Util::Message(messageId)
{
mData.Add(data1);
mData.Add(data2);
}
Array<UInt32> mData;
};
class MyThread : public Util::MessageThread
{
public:
enum MyMessageIds
{
MY_MESSAGE_1,
MY_MESSAGE_2
};
MyThread()
{
disp.Register<MyMessage1>(MY_MESSAGE_1, this, &MyThread::Receive1);
disp.Register<MyMessage2>(MY_MESSAGE_2, this, &MyThread::Receive2);
}
virtual ~MyThread()
{
}
{
SendMessage(AutoPointer<MyMessage1>(
new MyMessage1(MY_MESSAGE_1, data)));
}
{
SendSyncMessage(AutoPointer<MyMessage2>(
new MyMessage2(MY_MESSAGE_2, data1, data2)));
}
protected:
void Receive1(AutoPointer<MyMessage1> message)
{
}
void Receive2(AutoPointer<MyMessage2> message)
{
}
};
void MyMain()
{
MyThread myThread;
myThread.Start();
myThread.Send2(16, 17);
myThread.Send1(42);
myThread.Send2(19, 18);
myThread.Send1(43);
myThread.SendQuit();
myThread.Join();
}
◆ MessageThread()
| Murl::Util::MessageThread::MessageThread |
( |
const String & |
name | ) |
|
Constructor taking a thread name.
- Parameters
-
◆ ~MessageThread()
| Murl::Util::MessageThread::~MessageThread |
( |
| ) |
|
|
override |
◆ Start() [1/2]
| Bool Murl::Util::MessageThread::Start |
( |
| ) |
|
|
overridevirtual |
◆ Start() [2/2]
Create and start the thread, run the message loop with timeout.
If a timeout occurs the message dispatcher timeout method is called which executes the MessageDispatch::RegisterTimeout() callback object.
- Parameters
-
| timeout | The maximum time to wait for messages. |
- Returns
- System::Thread::Start().
◆ Stop()
| void Murl::Util::MessageThread::Stop |
( |
| ) |
|
|
overridevirtual |
◆ SetTimeout()
| void Murl::Util::MessageThread::SetTimeout |
( |
const System::Time & |
timeout | ) |
|
Set the message loop timeout.
- Parameters
-
| timeout | The maximum time to wait for messages. |
◆ GetTimeout()
| const System::Time& Murl::Util::MessageThread::GetTimeout |
( |
| ) |
const |
Get the message loop timeout.
- Returns
- The message loop timeout.
◆ GetMessageQueue()
| MessageQueue& Murl::Util::MessageThread::GetMessageQueue |
( |
| ) |
|
Get the thread's message queue object.
- Returns
- The thread's message queue object.
◆ GetMessageDispatch()
Get the thread's message dispatch object.
- Returns
- The thread's message dispatch object.
◆ SendQuit()
| virtual Bool Murl::Util::MessageThread::SendQuit |
( |
| ) |
|
|
virtual |
Send the ID_QUIT message to the thread.
- Returns
- true if successful.
◆ SendId()
| virtual Bool Murl::Util::MessageThread::SendId |
( |
UInt32 |
messageId | ) |
|
|
virtual |
Send the Message object to the thread.
- Parameters
-
| messageId | The message identifier to send. |
- Returns
- true if successful.
◆ SendMessage()
Send a message object to the thread.
- Parameters
-
| message | The message object to send. |
- Returns
- true if successful.
◆ SendSyncMessage()
Send a message object to the thread and wait until the message is delivered and processed.
If this method is called from it's own thread context the message is dispatched immediately.
- Parameters
-
| message | The message object to send. |
- Returns
- true if successful.
◆ WaitMessage() [1/2]
Wait for a message (using Message::ID_ANY).
- Parameters
-
| message | The message return value. The message returned is removed from the message queue. |
- Returns
- The message queue result.
◆ WaitMessage() [2/2]
Wait for a message with timeout (using Message::ID_ANY).
- Parameters
-
| message | The message return value. The message returned is removed from the message queue. |
| timeout | The maximum time to wait. |
- Returns
- The message queue result.
◆ GetMessage()
Get a message from the message queue (using Message::ID_ANY).
- Parameters
-
| message | The message return value. The message returned is removed from the message queue. |
- Returns
- The message queue result.
◆ PeekMessage()
Peek a message from the message queue (using Message::ID_ANY).
Checks if a message is in the message queue but does not remove the message from the message queue.
- Parameters
-
| message | The message return value. |
- Returns
- The message queue result.
◆ Run()
| Bool Murl::Util::MessageThread::Run |
( |
| ) |
|
|
overrideprotectedvirtual |
The System::Thread::Run() method implementation.
Implements the message loop:
{
{
{
}
else
{
}
{
if (!message.IsNull())
{
Debug::Error(
"MessageThread '%s': Non-dispatched message id %d received",
}
}
{
}
{
Thread::Stop();
}
}
return true;
}
- Returns
- true if successful.
Implements Murl::System::Thread.
◆ DispatchMessage() [1/2]
Wait for a message and dispatch the message.
- Parameters
-
| message | The message return value. |
- Returns
- The wait message queue result.
◆ DispatchMessage() [2/2]
Wait for a message and dispatch the message with timeout.
- Parameters
-
| message | The message return value. |
| timeout | The maximum time to wait. |
- Returns
- The wait message queue result.
The documentation for this class was generated from the following file:
- murl_util_message_thread.h
virtual void Flush()
Flush the autorelease memory pool.
MessageDispatch mMessageDispatch
The message dispatch instance.
Definition: murl_util_message_thread.h:304
void Error(const Char *message)
Write an error message string.
Definition: murl_debug_error.h:33
virtual Bool SendSyncMessage(Message::AutoPtr message)
Send a message object to the thread and wait until the message is delivered and processed.
virtual Bool SendMessage(Message::AutoPtr message)
Send a message object to the thread.
@ QUIT
Received the quit message.
Definition: murl_util_message_queue.h:45
String mName
The name of the thread.
Definition: murl_system_thread.h:157
System::Time mTimeout
The message loop timeout.
Definition: murl_util_message_thread.h:306
Result
Enumeration of the message queue results.
Definition: murl_util_message_queue.h:37
const Char * Begin() const
Get the pointer to the first character.
Definition: murl_string.h:1059
MessageThread()
The default constructor.
bool Bool
Boolean data type This typedef represents a boolean value (true or false).
Definition: murl_types.h:174
virtual MessageQueue::Result DispatchMessage(Message::AutoPtr &message)
Wait for a message and dispatch the message.
Bool IsInfinite() const
Check if the time is infinite.
MessageDispatch & GetMessageDispatch()
Get the thread's message dispatch object.
@ FAILED
Receiving a message failed.
Definition: murl_util_message_queue.h:39
void Trace(const Char *message)
Write a trace message string.
Definition: murl_debug_trace.h:48
Bool mIsRunning
The running state of the thread.
Definition: murl_system_thread.h:154
@ TIMEOUT
Timeout receiving a message.
Definition: murl_util_message_queue.h:43
@ RECEIVED
Received a message.
Definition: murl_util_message_queue.h:41
MurlUInt32 UInt32
Unsigned 32 bit integer data type.
Definition: murl_types.h:152
virtual Bool ExecuteTimeout()
Execute the timeout callback.
void Stop() override
Stop the thread.
AutoPointer< Message > AutoPtr
Definition of the Message class auto pointer.
Definition: murl_util_message.h:29
virtual void Join()
Join and destroy the thread.
Bool Run() override
The System::Thread::Run() method implementation.