Class Worker
- Namespace
- CSharpToJavaScript.APIs.JS
- Assembly
- CSharpToJavaScript.dll
The Worker interface of the Web Workers API represents a background task that can be created via script, which can send messages back to its creator.
[Value("Worker")]
public class Worker : EventTarget, AbstractWorker, MessageEventTarget
- Inheritance
-
Worker
- Implements
- Inherited Members
Remarks
Creating a worker is done by calling the Worker("path/to/worker/script") constructor.
Workers may themselves spawn new workers, as long as those workers are hosted at the same origin as the parent page.
Note that not all interfaces and functions are available to web workers. See Functions and classes available to Web Workers for details.
-Using Web Workers
-Functions and classes available to Web Workers
-Other kind of workers: SharedWorker and Service Worker.
-OffscreenCanvas interface
Constructors
Worker()
public Worker()
Worker(Union96, WorkerOptions)
WARNING
This script passed to theurlelement is executed.
APIs like this are known as injection sinks, and are potentially a vector for cross-site scripting (XSS) attacks.
public Worker(Union96 scriptURL, WorkerOptions options = null)
Parameters
scriptURLUnion96optionsWorkerOptions
Remarks
You can mitigate this risk by having a Content Security Policy (CSP) that restricts the locations from which scripts can be loaded, and by always assigning TrustedScriptURL objects instead of strings and enforcing trusted types.
See Security considerations for more information.
The Worker() constructor creates a Worker object that executes the classic script or module at the specified URL.
Methods
PostMessage(dynamic, StructuredSerializeOptions)
The postMessage() method of the Worker interface sends a message to the worker. The first parameter is the data to send to the worker. The data may be any JavaScript object that can be handled by the structured clone algorithm.
[Value("postMessage")]
public GlobalObject.Undefined PostMessage(dynamic message, StructuredSerializeOptions options = null)
Parameters
messagedynamicoptionsStructuredSerializeOptions
Returns
Remarks
The Worker postMessage() method delegates to the MessagePort PostMessage(dynamic, List<Object>) method, which adds a task on the event loop corresponding to the receiving MessagePort.
The Worker can send back information to the thread that spawned it using the PostMessage(dynamic, List<Object>) method.
-The Worker interface it belongs to.
PostMessage(dynamic, List<Object>)
The postMessage() method of the Worker interface sends a message to the worker. The first parameter is the data to send to the worker. The data may be any JavaScript object that can be handled by the structured clone algorithm.
[Value("postMessage")]
public GlobalObject.Undefined PostMessage(dynamic message, List<Object> transfer)
Parameters
Returns
Remarks
The Worker postMessage() method delegates to the MessagePort PostMessage(dynamic, List<Object>) method, which adds a task on the event loop corresponding to the receiving MessagePort.
The Worker can send back information to the thread that spawned it using the PostMessage(dynamic, List<Object>) method.
-The Worker interface it belongs to.
Terminate()
The terminate() method of the Worker interface immediately terminates the Worker. This does not offer the worker an opportunity to finish its operations; it is stopped at once.
[Value("terminate")]
public GlobalObject.Undefined Terminate()