# Worker

Worker Instance in the main thread, you can wx.createWorker Interface acquisition, worker A thread can use a global variable worker Get.

# method

# Worker.postMessage(Object message)

Main thread/Worker The message sent by the thread.

# Worker.terminate()

End current Worker Threads. Only in the main thread worker Object.

# Worker.onMessage(function callback)

Monitor main thread/Worker Event of a message sent by a thread to the current thread.

# Worker.onProcessKilled(function callback)

to monitor Worker thread is recycled by system event (when iOS system resource is tight, worker thread exists the possibility of system recycling, developer can listen to this event and create a worker again). Only in the main thread worker Object.

# sample code

Basic configuration is required to run the following code Mini Program multithreading | Multithreaded mini-games Documentation Learn the basics and configuration methods.

const worker = wx.createWorker('workers/request/index.js') // Filename designation worker Absolute path to the entry file

worker.onMessage(function (res) {
  console.log(res)
})
// Listen for worker reclaims event
worker.onProcessKilled(function () {
  console.log('worker has been killed')
  // Recreate a worker
  // wx.createWorker()
})

worker.postMessage({
  -what are you doing? 'hello worker'
})

worker.terminate()