Worker Class

A dedicated worker (https://html.spec.whatwg.org/multipage/workers.html#dedicated-workers-and-the-worker-interface): a script that runs in its own DedicatedWorkerGlobalScope, on its own thread, with its own event loop and its own script engine (ScriptEngine.CreateWorkerScriptEngine). The messages and the errors of the worker are fired at this object by the event loop of its owner.

public sealed class Worker : EventTarget

Remarks

The worker is terminated when Worker.Terminate is called, when the document of its owner is discarded, or when the browsing context of its owner is disposed. The event loop of the owner considers that an operation is in progress while the worker has a task to run.

See also

Inheritance
EventTarget Worker

Constructors

NameDescription
Worker(GlobalScope, string, WorkerOptions?)Creates a worker whose owner is owner (https://html.spec.whatwg.org/multipage/workers.html#dom-worker).
Worker(TrustedScriptUrl, WorkerOptions?)Creates a worker whose owner is the current global object (https://html.spec.whatwg.org/multipage/workers.html#dom-worker).
Worker(string, WorkerOptions?)Creates a worker whose owner is the current global object (https://html.spec.whatwg.org/multipage/workers.html#dom-worker).

Worker overloads

Worker(GlobalScope, string, WorkerOptions?)

Worker.cs

Creates a worker whose owner is owner (https://html.spec.whatwg.org/multipage/workers.html#dom-worker).

public Worker(GlobalScope owner, string scriptUrl, WorkerOptions? options = null)
Parameters
owner GlobalScope
The window or the worker that owns the worker.
scriptUrl string
The URL of the script, relative to the API base URL of the owner.
options WorkerOptions?
The options.
Exceptions
DomException
A SyntaxError when the URL is invalid.

Worker(TrustedScriptUrl, WorkerOptions?)

Worker.cs

Creates a worker whose owner is the current global object (https://html.spec.whatwg.org/multipage/workers.html#dom-worker).

public Worker(TrustedScriptUrl scriptUrl, WorkerOptions? options = null)
Parameters
scriptUrl TrustedScriptUrl
The URL of the script, created by a Trusted Types policy.
options WorkerOptions?
The options.
See also

Worker(string, WorkerOptions?)

Worker.cs

Creates a worker whose owner is the current global object (https://html.spec.whatwg.org/multipage/workers.html#dom-worker).

public Worker(string scriptUrl, WorkerOptions? options = null)
Parameters
scriptUrl string
The URL of the script, relative to the API base URL of the current global object.
options WorkerOptions?
The options.
Exceptions
InvalidOperationException
There is no current global object (no script is running).
DomException
A SyntaxError when the URL is invalid.
See also

Properties

NameDescription
OnErrorGets or sets the event handler of the error event.
OnMessageGets or sets the event handler of the message event.
OnMessageErrorGets or sets the event handler of the messageerror event.

OnError

Worker.cs

Gets or sets the event handler of the error event.

public Action<Event>? OnError { get; set; }

OnMessage

Worker.cs

Gets or sets the event handler of the message event.

public Action<Event>? OnMessage { get; set; }

OnMessageError

Worker.cs

Gets or sets the event handler of the messageerror event.

public Action<Event>? OnMessageError { get; set; }

Methods

NameDescription
PostMessage(object?, StructuredSerializeOptions?)Posts a message to the worker (https://html.spec.whatwg.org/multipage/workers.html#dom-worker-postmessage-options).
PostMessage(object?, IReadOnlyList<object>)Posts a message to the worker (https://html.spec.whatwg.org/multipage/workers.html#dom-worker-postmessage).
Terminate()Terminates the worker: its running script is aborted and its event loop ends (https://html.spec.whatwg.org/multipage/workers.html#dom-worker-terminate).

PostMessage overloads

PostMessage(object?, StructuredSerializeOptions?)

Worker.cs

Posts a message to the worker (https://html.spec.whatwg.org/multipage/workers.html#dom-worker-postmessage-options).

public void PostMessage(object? message, StructuredSerializeOptions? options = null)
Parameters
message object?
The message: a value of the script engine, a .NET primitive value or a serializable platform object, or null for undefined.
options StructuredSerializeOptions?
The objects to transfer.
See also

PostMessage(object?, IReadOnlyList<object>)

Worker.cs

Posts a message to the worker (https://html.spec.whatwg.org/multipage/workers.html#dom-worker-postmessage).

public void PostMessage(object? message, IReadOnlyList<object> transfer)
Parameters
message object?
The message: a value of the script engine, a .NET primitive value or a serializable platform object, or null for undefined.
transfer IReadOnlyList<object>
The objects to transfer.
See also

Terminate()

Worker.cs

Terminates the worker: its running script is aborted and its event loop ends (https://html.spec.whatwg.org/multipage/workers.html#dom-worker-terminate).

public void Terminate()