|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
(Publisher: John Wiley & Sons, Inc.)
Author(s): Rod Stephens
ISBN: 0471188816
Publication Date: 06/01/97
Chapter 13 AsyncServer
The QueryServer application described in Chapter 12 allows a user to display reports generated on one or more different computers, but it does not take full advantage of the capabilities of all the computers available. Even though the reports may be produced by different machines, only one process can be running at one time.
Control of the client and server processes in QueryServer is strictly synchronized. Each step follows the previous one in a steady, predictable way. The client requests data from the server and waits for a reply. When the server is finished, the client displays the results and awaits the users next command. Figure 13.1 shows a timeline of the events that occur during a synchronous call to the server. Notice that the client sits idle while the server processes the request.
FIGURE 13.1 The timeline of events during a synchronous call to a server.
In a more efficient system, the client would call the server and then turn to other tasks. When the server finished processing the request, the client would display the results. Figure 13.2 shows the timeline for this kind of asynchronous system.
While an asynchronous server is processing a request, the client can perform other tasks. For example, it can request services from a different server. If all of the servers run on different computers, all of them can work on their separate tasks at the same time. The tasks will be finished sooner so the client program will be able to display the results sooner.
For example, suppose the client needs to display 10 reports that take 10 minutes each to generate. Using synchronous servers, the program would take 100 minutes to produce and display all the reports. If the 10 reports are requested asynchronously on 10 different computers, all of the results will be ready approximately 10 minutes later.
AsyncServer uses these techniques to generate reports. (These techniques work in Visual Basic 4.0 but not in 5.0.) The following section, Using AsyncServer, describes the program from the users point of view. It explains how the user can request reports and how AsyncServer displays the results.
The Key Techniques section after that lists the main programming concepts demonstrated by AsyncServer. The rest of this chapter explains those concepts in detail.
Using AsyncServer
In appearance, AsyncServer is very similar to the QueryServer program described in Chapter 12. It, too, queries a report librarian to present the user with a list of reports. When the user selects a report, AsyncServer uses a report server to generate the report.
Unlike QueryServer, however, AsyncServer generates the reports asynchronously. After asking a report server for a report, AsyncServer is ready to process other user requests so the user can ask for another report before the first has finished.
FIGURE 13.2 The timeline of events during an asynchronous call to a server.
To make its behavior a bit more clear, AsyncServer presents status messages describing operations that are pending. Figure 13.3 shows the AsyncServer application after it has requested a report listing total purchases grouped by purchaser. When the report returns, the program will update the status message to indicate that the report has arrived.
Key Techniques
The following list describes the Visual Basic programming techniques QueryServer uses to process asynchronous requests. The rest of the chapter describes these techniques in detail.
- Building Asynchronous Servers in Visual Basic. Visual Basic does not provide an easy way to create asynchronous applications. This section explains how a program can use timer controls to mimic asynchronicity.
- Requesting Reports. When the user requests a report, the ReportList client begins the process of requesting a report. This section tells how ReportList gets report parameters from the user and how it actually requests the report. It also explains how the program can display forms that imitate modality.
- Generating Reports. After the client requests a report, the server processes the request. This section shows how the report servers generate reports asynchronously.
- Displaying Results. After the server has finished generating the report, it invokes a callback object to send the results to the client. This section explains how AsyncServer displays the report servers results.
FIGURE 13.3 AsyncServer after requesting a report.
Building Asynchronous Servers in Visual Basic
Visual Basic does not provide a simple method for calling servers asynchronously. With a little work a program can create a similar effect.
When the client first calls the server, it should pass the server an object. When the server is finished processing, it will invoke one of the functions defined by this object. Because this allows the server to call back to the client, this object is known as a callback object.
It does not matter what functions the callback provides as long as the client and server agree. For example, the callback object class might define a public DisplayText subroutine that takes as a parameter some text to display. When the server invokes DisplayText, the callback object could display the text in a message box on the client machine.
The client calls the server passing it a callback object. To create the asynchronous effect, the server creates a form containing a timer control. It activates the timer and exits. This returns control to the client so it can continue performing other tasks asynchronously.
When the timer activates, it begins by disabling itself so it cannot activate again. It then creates a new server object to perform the actual server processing. For a report server, this is the object that generates the report results.
This object must also unload the form that contains the timer control. This step is extremely important. A Visual Basic program will not stop if it contains a loaded form. If the timers form is not unloaded, the server program will continue to run even after it has finished serving the client. If clients invoke the server a dozen times, the server program will end up with a dozen forms loaded and wasting resources.
After the server object has unloaded the timers form and generated appropriate output, it calls the designated subroutine defined by the callback object to send the results back to the client.
Figure 13.4 shows the timeline of events during this process.
FIGURE 13.4 The timeline of events during an asynchronous call to a server using Visual Basic.
Requesting Reports
Much of the structure of AsyncServer is similar to that of the QueryServer application described in Chapter 12. Both applications use the same Librarian.ReportLister class to obtain a list of the available reports. The applications differ mainly in the ways they invoke the report servers and in how the servers work.
Both applications begin by requesting report parameters from the user. AsyncServer cannot present the parameter form modally, however, so the process is more complicated in the asynchronous version.
|