![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Advanced Visual Basic Techniques
In addition to the server class, this program has a form that will become the client application when the client and server are separated. The form contains two text boxes. A timer control periodically updates these text boxes so they display the date and time returned by a TimeSetter object. The form also contains two buttons, one that sets the servers time equal to the clients time, and one that sets the clients time equal to the servers time. This form is shown in design mode in Figure 11.3. The following code shows the timer and command button event handlers used by this application. You can find the complete application in the Ch11\Step1 subdirectory on the compact disk.
Option Explicit
Dim time_setter As New TimeSetter
Private Sub CmdSetClientTime_Click()
Time = time_setter.GetTime
Date = time_setter.GetDate
End Sub
Private Sub CmdSetServerTime_Click()
time_setter.SetTime Time
time_setter.SetDate Date
End Sub
Private Sub TimeTimer_Timer()
TimeLabel.Caption = Format$(time_setter.GetTime)
DateLabel.Caption = Format$(time_setter.GetDate)
End Sub
The time and date synchronization subroutines are quite simple. They do not take into account the delay caused by accessing the server application, and they do not compensate for additional time that may be lost due to a heavy network load. These functions are sufficient for synchronizing the clocks of the client and server computers to within a second or so.
In this case, the two synchronization buttons do not do much. Because the server object runs within the client application, the client and server computers are the same machine. That means both of these buttons synchronize the computer with itself. Some server functions such as these cannot be completely tested until the client and server are running on separate machines. At this point, however, it is still worth stepping through the code in the debugger to test the server as much as possible. Step 2: Testing LocallyOnce the application code has been thoroughly debugged within a single application, it should be broken into separate client and server applications. These two pieces should then be tested within two instances of the Visual Basic development environment running on the same computer. Figure 11.4 shows how the client and server applications interact schematically. When the client references the svr object, it is created in the server application and runs within the server application. The fact that the client and server both run on the same computer simplifies server installation and testing. The sections that follow explain how to build the client and server applications and how to test them on the same computer. The code for the step 2 TimeSync client and server programs is contained in the Client and Server subdirectories of the Ch11\Step2 directory on the compact disk. Building the ServerA server for a client/server application is an OLE (ActiveX) server. As is the case for the Add-Ins described in Chapters 4 through 7, these OLE servers must provide some additional information so they can be correctly identified by the system. The necessary project properties are described in detail in Chapter 4, so they are listed briefly here. Project Properties in Visual Basic 4 In Visual Basic 4, these properties are set by selecting the Options command on the Tools menu. The Project tab of the Options dialog is shown in Figure 11.5. Three options must be set on the Project Options dialog to create an add-in: Startup Form, Project Name, and StartMode.
For Visual Basic 4, the applications Startup Form should be set to Sub Main. The Main subroutine need not do anything unless the server must perform some sort of initialization at startup, but it must be present. The following code shows the Main subroutine for step 2 of the TimeSync project. Sub Main() End Sub The Project Name option determines the name by which the server will be known to the system registry. Other applications will use this name to access the server. For example, in this case the projects name is TimeManager, and the timer server class is called TimeSetter. The client application will refer to this class as TimerManager.TimeSetter. Finally, the programs StartMode should be set to OLE Server. Project Properties in Visual Basic 5 In Visual Basic 5, the Properties command in the Project menu presents the Project Options dialog. The General tab of the Project Properties dialog is shown in Figure 11.6. In Visual Basic 5, only two options must be set on the Project Properties dialog: Project Type and Project Name. Project Type should be set to ActiveX EXE. The Project Name should be set as it is for Visual Basic 4. The Startup Object can be set either to Sub Main or (None). If it is set to Sub Main, the program must include a subroutine named Main. The Server Class Because the step 1 program used a separate server class to provide server functions, building the server program is easy. The server class from step 1 is simply added to the server program project. All the Visual Basic code should remain the same. Only a few changes to the properties of the class are required. In Visual Basic 4, the Public property of the class should be set to true. Its Instancing variable should be set to Creatable SingleUse (1) or Creatable MultiUse (2). In Visual Basic 5, the Instancing variable of the server class should be set to MultiUse (5). Running the Server At this point, the server is ready to run. When the server program is executed in the Visual Basic design environment, the program should start and then appear to do nothing. In Visual Basic 4, if the program immediately exits, the projects StartMode is probably not set to OLE Server. In Visual Basic 5, the Project Type option is probably not set to ActiveX EXE. When the client application later executes, it will invoke the running server application. Break points in the client and server allow the execution to be studied and debugged.
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-1999 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permision of EarthWeb is prohibited.
|