Type/to search
Built-in Functions
Global
Log
Market
Trade
Account
Futures
Exchange
IO
Network
Storage
Threads
exchange.Go
EventLoop
ctx.subscribe
ctx.poll
ctx.book
threading
Thread
Thread.peekMessage
Thread.postMessage
Thread.join
Thread.terminate
Thread.getData
Thread.setData
Thread.id
Thread.name
Thread.eventLoop
ThreadLock
ThreadEvent
ThreadCondition
ThreadDict
Server
TA
Talib
OS
Structures
Built-in Variables and Constants
Conventions
Exchanges

Sends messages to a thread.

postMessage(msg)

Examples

  • Send messages in concurrent threads and use eventLoop() to receive message notifications.

    javascript
    function 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.

    javascript
    function 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

NameTypeRequiredDescription

msg

string / number / bool / object / array / function / any

Yes

The parameter msg is the message to be sent.

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.