Thread.postMessage
向线程发送消息。
postMessage(msg)示例
-
在并发线程中发送消息,使用
eventLoop()接收消息通知。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() } -
支持发送函数。
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() }
参数
| 名称 | 类型 | 必填 | 描述 |
msg | string / number / bool / object / array / function / any | 是 | 要发送的消息。 |
参考
备注
当线程的执行函数中调用postMessage()函数发送信号或数据时,会产生消息事件。可以使用eventLoop()函数接收消息通知。
每个线程的消息队列最多保存10240条消息、合计32MB,任一上限到达时丢弃最旧的消息并输出一条警告。接收消息的线程应及时调用peekMessage()取走消息。