SharedWorker Class

A shared worker (https://html.spec.whatwg.org/multipage/workers.html#shared-workers-and-the-sharedworker-interface): the SharedWorker objects of the same browsing context group with the same script URL and name (and origin) are connected to the same SharedWorkerGlobalScope, which runs on its own thread. Each object communicates with the worker through its SharedWorker.Port.

public sealed class SharedWorker : EventTarget

Remarks

A shared worker lives until it calls close() or its browsing context group is disposed.

See also

Inheritance
EventTarget SharedWorker

Constructors

NameDescription
SharedWorker(GlobalScope, string, SharedWorkerOptions?)Connects to a shared worker whose owner is owner (https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker).
SharedWorker(TrustedScriptUrl, SharedWorkerOptions?)Connects to a shared worker, created if needed (https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker).
SharedWorker(TrustedScriptUrl, string)Connects to a shared worker, created if needed (https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker).
SharedWorker(string, SharedWorkerOptions?)Connects to a shared worker, created if needed (https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker).
SharedWorker(string, string)Connects to a shared worker, created if needed (https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker).

SharedWorker overloads

SharedWorker(GlobalScope, string, SharedWorkerOptions?)

Worker.cs

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

public SharedWorker(GlobalScope owner, string scriptUrl, SharedWorkerOptions? options = null)
Parameters
owner GlobalScope
scriptUrl string
options SharedWorkerOptions?

SharedWorker(TrustedScriptUrl, SharedWorkerOptions?)

Worker.cs

Connects to a shared worker, created if needed (https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker).

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

SharedWorker(TrustedScriptUrl, string)

Worker.cs

Connects to a shared worker, created if needed (https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker).

public SharedWorker(TrustedScriptUrl scriptUrl, string name)
Parameters
scriptUrl TrustedScriptUrl
The URL of the script, created by a Trusted Types policy.
name string
The name of the worker.
See also

SharedWorker(string, SharedWorkerOptions?)

Worker.cs

Connects to a shared worker, created if needed (https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker).

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

SharedWorker(string, string)

Worker.cs

Connects to a shared worker, created if needed (https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker).

public SharedWorker(string scriptUrl, string name)
Parameters
scriptUrl string
The URL of the script, relative to the API base URL of the current global object.
name string
The name of the worker.
See also

Properties

NameDescription
OnErrorGets or sets the event handler of the error event.
PortGets the port connected to the connect event of the worker (https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker-port).

OnError

Worker.cs

Gets or sets the event handler of the error event.

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

Gets the port connected to the connect event of the worker (https://html.spec.whatwg.org/multipage/workers.html#dom-sharedworker-port).

public MessagePort Port { get; }