The postMessage()
function is used to send a message to a thread.
postMessage(msg)
The parameter msg
is the message to be sent.
msg true Any type supported by the system, such as string, number, bool, object, array, function, null value, etc.
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()
}
Send messages in concurrent threads and use eventLoop()
to receive message notifications.
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()
}
It supports sending a function.
When a thread’s execution function calls the postMessage()
function to send a signal or data, a message event is also generated. We can use the eventLoop()
function to receive message notifications.
{@fun/Threads/Thread/peekMessage peekMessage}, {@fun/Threads/Thread/join join}, {@fun/Threads/Thread/terminate terminate}, {@fun/Threads/Thread/getData getData}, {@fun/Threads/Thread/setData setData}, {@fun/Threads/Thread/id id}, {@fun/Threads/Thread/name name}, {@fun/Threads/Thread/eventLoop eventLoop}
peekMessage join