Skip to content

NodeWorker class

A Node.js worker thread, as documented at https://nodejs.org/api/worker_threads.html.

C#
public class NodeWorker

Public Members

namedescription
NodeWorker(…)Creates a new instance of NodeWorker that runs a Node.js script in a separate worker thread. (2 constructors)
Stderr { get; }A readable stream which contains data written to !:NodeProcess.Stderr inside the worker thread. If Stderr: true was not passed to the Worker constructor, then data is piped to the parent thread's !:NodeProcess.Stderr stream.
Stdin { get; }If Stdin: true was passed to the Worker constructor, this is a writable stream. The data written to this stream will be made available in the worker thread as Stdin.
Stdout { get; }A readable stream which contains data written to Stdout inside the worker thread. If Stdout: true was not passed to the Worker constructor, then data is piped to the parent thread's Stdout stream.
ThreadId { get; }An integer identifier for the referenced thread. Inside the worker thread, it is available as CurrentThreadId. This value is unique for each Worker instance inside a single process.
event ErrorEmitted if the worker thread throws an uncaught exception. In that case, the worker is terminated.
event Exit
event Message
event MessageErrorEmitted when deserializing a message failed.
event OnlineEmitted when the worker thread has started executing JavaScript code.
PostMessage(…)Send a message to the worker that is received via the ParentPortMessage event.
Ref()Opposite of Unref. Calling ref() on a previously unref()ed worker does not let the program exit if it's the only active handle left (the default behavior). If the worker is ref()ed, calling ref() again has no effect.
Terminate()Stops all JavaScript execution in the worker thread as soon as possible. Returns a Promise for the exit code that is fulfilled when the 'exit' event is emitted.
Unref()Allows the thread to exit if this is the only active handle in the event system. If the worker is already unref()ed calling unref() again has no effect.
static CurrentThreadId { get; }Gets an integer identifier for the current JS thread. On the corresponding worker object (if there is any), it is available as ThreadId. This value is unique for each instance inside a single process.
static CurrentWorkerData { get; }An arbitrary JavaScript value that contains a clone of the data passed to this thread's Worker constructor.
static IsMainThread { get; }Gets a value indicating whether the current code is running on the main JS thread; if false it is a worker thread.
static ParentPort { get; }
static GetEnvironmentData(…)Within a worker thread, returns a clone of data passed to the spawning thread's SetEnvironmentData. Every new Worker receives its own copy of the environment data automatically.
static SetEnvironmentData(…)Sets the environment data in the current thread and all new Worker instances spawned from the current context.
class ErrorEventArgs
class ExitEventArgs
class MessageEventArgs
class MessagePortRepresents one end of an asynchronous, two-way communications channel. It can be used to transfer structured data, memory regions, and other MessagePorts between different Workers.
class OptionsOptions for configuring a NodeWorker.
class ResourceLimitsResource limits for a NodeWorker.

Remarks

Static members of this class enable a worker thread to access its current context, while instance members enable a parent (or main) thread to manage a specific child worker thread.

See Also

Released under the MIT license