HTML and CSS Reference
■ For the demo application that you'll create in this chapter, the messages between the caller and the worker
will be simple text messages. However they can be any format you want, including JSOn encoded data.
One of the most significant limitations of web workers is that they cannot access the DOM, so you can't use
them to update the page content or style. Also, they cannot access the window object, which means, among other
things, that you can't use timers. With these limitations in mind you might be wondering when you would use a
Web workers are great for performing tasks such as retrieving data. For example, if you need to lookup
information from an external source (such as a database, local file system, or web) you can pass the lookup
parameters to the worker and when the lookup finishes the data can be passed back as a JSON message. This
allows the web page to respond to user actions while the data is being retrieved.
Web workers come in two varieties: dedicated and shared. A single shared worker can be used by multiple
web pages. Both dedicated and shared workers function the same basic way but the communication is a little
different. You'll start by implementing a dedicated web worker.
Using a Dedicated Worker
A dedicated web worker, as its name implies, is dedicated to the web page that created it. The web page creates it,
uses it as needed and closes it when it no longer needs it. A web page can create as many workers as it needs.
To demonstrate a dedicated web worker, you'll build a simple web page that will allow you to create a worker
and send messages to it. It will also display the response so you can see the two-way communication. The worker
implementation is trivial; simply echoing back the message that was sent to it.
eXerCISe 5-1. USING a DeDICateD WeB WOrKer
in the Chapter
5 project that you created earlier, right-click the Controller folder
in the Solution Explorer and select the Add ➤ Controller links. Enter the name
HomeController and select the Empty mVC controller template. Click the Add button
to create the controller.
Right-click the View folder and select the Add
➤ new Folder links. Enter the name
Right-click the new Home folder and select the Add
➤ View links. in the Add View
dialog box, enter the name Index , make sure the Razor view engine is selected, and
unselect all the check boxes as shown in Figure 5-3 .