Skip to main content

WebWorker

Namespace: Wisej.Ext.WebWorker

Assembly: Wisej.Ext.WebWorker (4.1.0.0)

The WebWorker component represents a JavaScript WebWorker instance that can run on the client and fire sever events and receive updates from the server.

public class WebWorker : Component, IWisejHandler

The worker script is set using either the JavaScript or the JavaScriptSource property and runs in a background thread in the browser. Data sent from the server with SendMessage is received in the worker's onmessage handler, and data sent by the worker with postMessage fires the PostMessage event on the server. See: Web Workers API.

Example:

The following example creates a worker that doubles the numbers it receives and sends the result back to the server:

private WebWorker webWorker;

private void Page1_Load(object sender, EventArgs e)
{
this.webWorker = new WebWorker(this.components);
this.webWorker.JavaScript = @"
onmessage = function (e) {
postMessage(e.data * 2);
};";

this.webWorker.PostMessage += webWorker_PostMessage;
}

private void buttonCalculate_Click(object sender, EventArgs e)
{
this.webWorker.SendMessage(21);
}

private void webWorker_PostMessage(object sender, WebWorkerPostMessageEventArgs e)
{
AlertBox.Show($"Result: {e.Data}"); // Result: 42
}

Constructors​

Instance member WebWorker()​

Initializes a new instance of the WebWorker class.

Example:

var worker = new WebWorker();
worker.JavaScriptSource = "Scripts/worker.js";
worker.PostMessage += (s, e) => AlertBox.Show(e.Data?.ToString());

Instance member WebWorker(container)​

Initializes a new instance of the WebWorker class together with the specified container.

NameTypeDescription
containerIContainerA IContainer that represents the container for the component.

Adding the component to a container ensures that it's disposed together with the container, for example when the owning page or form is disposed.

Throws:

Example:

// "components" is the container created by the designer for the page or form.
var worker = new WebWorker(this.components);
worker.JavaScript = "onmessage = function (e) { postMessage('Received: ' + e.data); };";

Properties​

Static member IsSupported​

Boolean: Returns true if the client browser supports the JavaScript Worker class.

Instance member JavaScript​

String: Returns or sets the JavaScript code to execute in the WebWorker process. (Default: "")

Instance member JavaScriptSource​

String: Returns or sets the JavaScript file with the source code to execute in the WebWorker process. (Default: "")

Methods​

Protected member OnPostMessage(e)​

Fires the PostMessage event.

ParameterTypeDescription
eWebWorkerPostMessageEventArgs

Protected member OnWebEvent(e)​

Processes the event from the client.

ParameterTypeDescription
eWisejEventArgsEvent arguments.

Protected member OnWebRender(config)​

Renders the client component.

ParameterTypeDescription
configObjectDynamic configuration object.

Instance member SendMessage(data)​

Sends the data object to the current WebWorker, if it's running.

ParameterTypeDescription
dataObjectThe data object to send to the WebWorker. It's serialized to JSON before it's sent to the client.

If the worker is not running yet, it's started before the data is sent. The worker receives the data in the data property of the event passed to its onmessage handler.

Example:

The following example sends an object to a worker that sums an array of numbers:

// worker script:
// onmessage = function (e) {
// var sum = e.data.values.reduce(function (a, b) { return a + b; }, 0);
// postMessage({ name: e.data.name, sum: sum });
// };

private void buttonSum_Click(object sender, EventArgs e)
{
this.webWorker.SendMessage(new
{
name = "Totals",
values = new[] { 1, 2, 3, 4, 5 }
});
}

private void webWorker_PostMessage(object sender, WebWorkerPostMessageEventArgs e)
{
dynamic result = e.Data;
AlertBox.Show($"{result.name}: {result.sum}"); // Totals: 15
}

Instance member Terminate()​

Terminates the current WebWorker.

The worker process in the browser is stopped immediately, without letting it finish its current work. A new worker is started automatically the next time SendMessage is called or when the JavaScript or JavaScriptSource property changes.

Example:

The following example stops a long running worker when the user clicks a cancel button:

private void buttonCancel_Click(object sender, EventArgs e)
{
this.webWorker.Terminate();
this.labelStatus.Text = "Canceled.";
}

Instance member Update()​

Updates the component on the client.

Calling this method reloads the worker's source code on the client, which terminates the running worker and starts a new one. It's called automatically when the JavaScript or JavaScriptSource property changes.

Example:

The following example restarts the worker after the script file has been changed on the server:

private void buttonReload_Click(object sender, EventArgs e)
{
File.WriteAllText(
Path.Combine(Application.StartupPath, "Scripts/worker.js"),
"onmessage = function (e) { postMessage(e.data.toUpperCase()); };");

this.webWorker.Update();
}

Events​

Instance member PostMessage​

WebWorkerPostMessageEventHandler Fired when the WebWorker calls "postMessage".