HTML and CSS Reference
In-Depth Information
port object, in the current implementation of shared web workers there is always only one port in the
ports array; thus you access the port object at array index 0.
The addEventListener() method wires an event-handler function— SendMessageToPage() —for the
port object's message event. The start() method of the port object starts the communication channel.
n Note Although it isn't used in this example, the port object also has a close() method. This method closes a
port and stops any further communication on that port.
The SendMessageToPage() event-handler function contains the same processing logic ( do - while loop)
as before. To send the result of the processing back to the web form, postMessage() is called on the port
object. The string message sent by the web form is retrieved using the evt.data property, and extra string
data is appended to it.
If you run the web form and click the Start Work button, you should see an alert box as shown in
Figure 10-6.
Figure 10-6. Shared web worker in action
Figure 10-6 shows that the script is being executed properly by the shared web worker. If you wish to
see the effect on thread utilization of using shared web workers, open the Resource Monitor and look at
the entries for Chrome.exe as before (Figure 10-7).
Figure 10-7 shows the Resource Monitor when the web form is open in two Chrome tabs. In all, there
are four active entries for Chrome.exe . The one with 29 threads is for the main browser window. Every open
tab takes 6 threads. So, the 2 entries with 6 threads each are for the 2 tabs in which the web form is loaded.
Another set of 6 threads is created when you click the Start Work button for the first time; they're used for
running the shared web workers. If you click Start Work multiple times, there is no further increase in the
thread count because the previously created shared worker threads are reused by subsequent requests.
n Note Subsequent executions of the script running in a shared web worker get the previous state of the global
variables. Also, if a shared web worker is already being used by a page, other pages interested in gaining access to
the same shared web worker need to wait until the current processing is complete.
 
Search WWH ::




Custom Search