HTML and CSS Reference
In-Depth Information
Terminating Web Workers
When you trigger a lengthy operation on a web worker, the only way you hear back from the worker thread
is when it sends you the result of processing using the
postMessage()
method or when an error takes place
during the execution of the worker thread. If the operation being executed on the worker thread is long-
running, you may wish to allow the user to cancel the operation before its completion. The
Worker
object's
terminate()
method allows you to do that. The following piece of code shows how you use
terminate()
:
$("#btnStop").click(function () {
worker.terminate();
});
This code handles the
click
event of a button (
btnStop
) and cancels the operation running on the
worker thread by calling
terminate()
. Once a web worker is terminated, you can't reuse or restart it. The
only way to restart the operation is to create a new
Worker
object.
Monitoring Web Workers During Development
As an ASP.NET developer, you interact with web workers using the
Worker
object. You don't need to know
the internal details of how the browser creates and manages the threads. However, during the
development and testing phase, you may want to see the effect of using web workers on the number of
threads created by the browser. If so, you can use the Windows Resource Monitor to peek inside the
browser's internal thread handling. You can access the Resource Monitor from the Performance tab of the
Task Manager dialog.
Figure 10-5 shows entries for
Chrome.exe
in the Resource Monitor when the web form you developed
earlier is loaded in Chrome.
Figure 10-5.
Resource Monitor showing thread utilization