Thread.postMessage
Sends messages to a thread.
postMessage(msg)Examples
-
Send messages in concurrent threads and use
eventLoop()to receive message notifications.javascriptfunction main() { var t1 = threading.Thread(function() { for (var i = 0; i < 10; i++) { Log("thread1 postMessage():", i) threading.mainThread().postMessage(i) Sleep(500) } }) for (var i = 0; i < 10; i++) { var event = threading.mainThread().eventLoop() Log("main event:", event) Sleep(500) } t1.join() } -
Supports sending functions.
javascriptfunction main() { threading.mainThread().postMessage(function(msg) { Log("func from mainThread, msg:", msg) }) threading.Thread(function() { var func = threading.mainThread().peekMessage() func("in " + threading.currentThread().name()) }).join() }
Arguments
| Name | Type | Required | Description |
msg | string / number / bool / object / array / function / any | Yes | The parameter |
See Also
Remarks
When the postMessage() function is called in a thread's execution function to send signals or data, it generates a message event. You can use the eventLoop() function to receive message notifications.
Each thread's message queue holds at most 10240 messages and 32 MB in total; when either limit is reached, the oldest messages are dropped and a warning is printed. The receiving thread should call peekMessage() regularly to take messages off the queue.