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 : EventTargetRemarks
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
EventTargetWorker
Constructors
| Name | Description |
|---|---|
| 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.csCreates 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
ownerGlobalScope- The window or the worker that owns the worker.
scriptUrlstring- The URL of the script, relative to the API base URL of the owner.
optionsWorkerOptions?- The options.
Exceptions
DomException- A
SyntaxErrorwhen the URL is invalid.
Worker(TrustedScriptUrl, WorkerOptions?)
Worker.csCreates 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
scriptUrlTrustedScriptUrl- The URL of the script, created by a Trusted Types policy.
optionsWorkerOptions?- The options.
See also
Worker(string, WorkerOptions?)
Worker.csCreates 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
scriptUrlstring- The URL of the script, relative to the API base URL of the current global object.
optionsWorkerOptions?- The options.
Exceptions
InvalidOperationException- There is no current global object (no script is running).
DomException- A
SyntaxErrorwhen the URL is invalid.
See also
Properties
| Name | Description |
|---|---|
| OnError | Gets or sets the event handler of the error event. |
| OnMessage | Gets or sets the event handler of the message event. |
| OnMessageError | Gets or sets the event handler of the messageerror event. |
OnError
Worker.csGets or sets the event handler of the error event.
OnMessage
Worker.csGets or sets the event handler of the message event.
OnMessageError
Worker.csGets or sets the event handler of the messageerror event.
Methods
| Name | Description |
|---|---|
| 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.csPosts 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
messageobject?- The message: a value of the script engine, a .NET primitive value or a serializable platform object, or null for
undefined. optionsStructuredSerializeOptions?- The objects to transfer.
See also
PostMessage(object?, IReadOnlyList<object>)
Worker.csPosts 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
messageobject?- The message: a value of the script engine, a .NET primitive value or a serializable platform object, or null for
undefined. transferIReadOnlyList<object>- The objects to transfer.
See also
Terminate()
Worker.csTerminates 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()