Thread.eventLoop
Listens for events received by the current thread (the thread of the caller).
eventLoop()
eventLoop(timeout)Examples
Concurrently execute 3 threads, outputting the received event information; when a timeout occurs or it returns immediately, the output is null.
javascript
function main() {
var t1 = threading.Thread(function() {
while (true) {
var eventMsg = threading.currentThread().eventLoop() // Block and wait
Log(_D(), "thread1 eventMsg:", eventMsg)
// Output: 2024-11-14 10:14:18 thread1 eventMsg: {"Seq":1,"Event":"thread","ThreadId":0,"Index":1,"Queue":0,"Nano":1731550458699947000}
}
})
var t2 = threading.Thread(function() {
while (true) {
var eventMsg = threading.currentThread().eventLoop(-1) // Return immediately
Log(_D(), "thread2 eventMsg:", eventMsg)
Sleep(5000)
}
})
var t3 = threading.Thread(function() {
while (true) {
var eventMsg = threading.currentThread().eventLoop(3000) // Set a 3-second timeout
Log(_D(), "thread3 eventMsg:", eventMsg)
}
})
t1.postMessage("Hello " + t1.name())
t2.postMessage("Hello " + t2.name())
t3.postMessage("Hello " + t3.name())
t1.join()
t2.join()
t3.join()
}Returns
| Type | Description |
object / null value | Event information received by the current thread. For details, see |
Arguments
| Name | Type | Required | Description |
timeout | number | No | Timeout duration, in milliseconds. If |
See Also
Remarks
The processing mechanism of the eventLoop() function is consistent with that of the global function EventLoop(). It always listens for events of the caller's thread, regardless of which Thread object it is called on; for example, calling t1.eventLoop() in the main thread returns main-thread events.