Module 9 - Developing Components in Visual Basic
Module 9 - Developing Components in Visual Basic
NET
Contents Overview Components Overview Creating Serviced Components Demonstration: Creating a Serviced Component Lab 9.1: Creating a Serviced Component Creating Component Classes Demonstration: Creating a Stopwatch Component Creating Windows Forms Controls Demonstration: Creating an Enhanced TextBox Creating Web Forms User Controls Demonstration: Creating a Simple Web Forms User Control Lab 9.2: Creating a Web Forms User Control Threading Demonstration: Using the SyncLock Statement Review 1 2 11 27 28 35 40 41 48 49 53 54 60 73 74
This course is based on the prerelease version (Beta 2) of Micro soft Visual Studio .NET Enterprise Edition. Content in the final release of the course may be different from the content included in this prerelease version. All labs in the course are to be completed with the Beta 2 version of Visual Studio .NET Enterprise Edition.
Information in this document, including URL and other Internet Web site references, is subject to change without notice. Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, e-mail address, logo, person, place or event is intended or should be inferred. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property. 2001 Microsoft Corporation. All rights reserved. Microsoft, MS-DOS, Windows, Windows NT, ActiveX, BizT alk, FrontPage, IntelliSense, JScript, Microsoft Press, Outlook, PowerPoint, Visio, Visual Basic, Visual C++, Visual C#, Visual InterDev, Visual Studio, and Windows Media are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries. The names of actual companies and products mentioned herein may be the trademarks of their respective owners.
iii
Instructor Notes
Presentation: 90 Minutes Labs: 90 Minutes This module provides students with the knowledge required to create and use different types of components by using Microsoft Visual Basic .NET. In the first lab, students will create a serviced component that retrieves customer information based on the customer e-mail address and password. s The assembly is installed in the Global Assembly Cache (GAC) to avoid a potential bug in Beta 2. This bug does not allow server-activated applications to be called successfully without being installed in the GAC. Students will register this component with Component Services and set constructor string properties of the component by using the Component Services console. They will then test the serviced component with a simple Windows-based application. In the second lab, students will create a Web user control that requests logon information from a customer. They will place the user control on a Web Form and use the serviced component that they created in the first lab to retrieve the customer information so that it can be displayed on a welcome screen. After completing this module, students will be able to:
n
Describe the different types of components that they can create in Visual Basic .NET. Create components that can be used by managed and unmanaged client applications. Create serviced components. Create component classes. Create Microsoft Windows Forms controls. Create Web Forms user controls. Use threading to create multithreaded applications.
n n n n n
iv
Required Materials
To teach this module, you need the following materials:
n n n n
Microsoft PowerPoint file 2373A_09.ppt Module 9, Developing Components in Visual Basic .NET Lab 9.1, Creating a Serviced Component Lab 9.2, Creating a Web Forms User Control
Preparation Tasks
To prepare for this module:
n n
Read all of the materials for this module. Complete the labs.
Demonstrations
This section provides demonstration procedures that will not fit in the margin notes or are not appropriate for the student notes.
vi
vii
2. In Solution Explorer, drag the TIMER01.ICO file and place it between the string quotes in the ToolboxBitmap("") code. Point out that adding the bitmap as an assembly resource may be a better approach as it will not rely on the icon file being available in the correct location. However, for this demonstration, this approach is acceptable.
viii
3. Click the Undo button four times, and view the changes in each text box. 4. Close the form and quit Visual Studio .NET.
ix
Creating a Simple Web Forms User Control To create the ASP .NET Web application
1. Open Visual Studio .NET. 2. Create a new ASP .NET Web application called SimpleUserControl in the http://localhost/2373/DemoCode/Mod09/SimpleUserControl folder.
3. Add the following attribute to the tag to set the LabelValue property of the control:
LabelValue="Enter a value:"
4. Return to the Design view, drag a Button control from the Toolbox to the Web Form, and place it well below the SimpleControl. Change the following properties of the Button control to the following values.
Button property Text (ID) Value Get Value btnGet
5. In the Code Editor for WebForm1, add the following variable declaration immediately after the button declaration:
Protected WithEvents SimpleControl1 As SimpleControl
6. Create a Click event handler for btnGet and enter the following code:
Response.Write(SimpleControl1.TextValue)
xi
Module Strategy
Use the following strategy to present this module:
n
Components Overview This lesson provides an overview of creating components with Visual Basic .NET, and of how they can work with both managed and unmanaged clients. Many students will be interested in how a Visual Basic 6.0 based client application can use a Visual Basic .NET component, so spend some time on the topic of using components in unmanaged client applications. Strongnamed assemblies are covered in Module 10, Deploying Applications, in Course 2373A, Programming with Microsoft Visual Basic .NET (Prerelease), but a simple explanation will be required in this lesson. The lesson ends with an overview of Microsoft .NET Remoting. Do not discuss this topic in detail. Instead, refer students to the Visual Studio .NET documentation or other to courses, such as Module 13, Remoting and Web Services, in Course 2349A, Programming the .NET Framework with C# (Prerelease) .
Creating Serviced Components This lesson relies on the studentsknowledge of MTS or Component Services. Ensure that all students have some awareness of these technologies, and briefly review the overall purpose of serviced components, such as providing predefined functionality for developers to use. You will then examine several aspects of serviced components, including transactions, object pooling, constructor strings, and security. Most of these should not be new to MTS or Component Services developers, but the way that the information is specified has changed. Object pooling is a significant enhancement for Visual Basic, and will be new to many students because previous versions of Visual Basic could not take advantage of this technique. Use the demonstration at the end of the lesson to reinforce this concept. End the lesson with an overview of other services provided by Component Services and of how to configure an assembly to run as a serviced application. This information will minimize the amount of administration required when installing Visual Basic .NET serviced applications. Note that because of a bug with Beta 2, attempting to use a Component Services application as Server activation causes an error unless the assembly is installed in the GAC. The demonstrations and labs have been modified to install the assemblies in the GAC to avoid this error.
Creating Component Classes This lesson examines component classes and their uses within a Visual Basic .NET based application. Students will learn about the architecture of a component class and how to create one. Although this is a small lesson, make sure students recognize the benefits of these types of components.
xii
Creating Windows Forms Controls This lesson relies on students having created Windows user controls in previous versions of Visual Basic. Discuss some of the ways user controls can be created and how attributes can be specified to assist other developers who use the user controls. There are other ways to create controls (such as creating controls from scratch) that are not covered because of time constraints. Direct students to the product documentation for further information regarding these topics. Creating Web Forms Controls This lesson examines how to create Web Forms user controls within an ASP .NET Web application. The lesson is quite short as the creation of these controls is very similar to the creation of Web Forms and Windows Forms user controls. Point out to students that they can also create user controls based on Web Forms simply by copying the controls from the Web Form to the Web user control. There are other types of Web user controls called Custom controls that are not covered in this course. Refer students to the online help documentation for more information.
Threading This lesson examines basic concepts of using threading in a Visual Basic .NET based application. Most Visual Basic students are not familiar with threads, so be sure to explain the basic concepts first. Discuss the advantages of using threading and how to create and use threads in a Visual Basic .NET based application. The lesson ends by raising some of the issues that students must be aware of when using threads. Be sure to point out that threading is a powerful yet potentially dangerous technique, and that students can choose whether or not to use it in their applications.
Overview
Topic Objective
To provide an overview of the module topics and objectives.
n n n n n n
Components Overview Creating Serviced Components Creating Component Classes Creating Windows Forms Controls Creating Web Forms User Controls Threading
Lead-in
In this module, you will learn how to create components in Visual Basic .NET.
As a Microsoft Visual Basic developer, you probably already know how to develop and use components in your applications. In Visual Basic .NET version 7.0, you can use the new design-time features to easily create components and extend their functionality. After completing this module, you will be able to:
n
Describe the different types of components that you can create in Visual Basic .NET. Create components that can be used by managed and unmanaged client applications. Create serviced components. Create component classes. Create Microsoft Windows Forms controls. Create Web user controls. Use threading to create multithreaded applications.
n n n n n
u Components Overview
Topic Objective
To provide an overview of the topics covered in this lesson.
n n n n n
Types of Components Using Modules As Components Using Classes As Components Using Components in Unmanaged Client Applications .NET Remoting Overview
Lead-in
This lesson explains the types of components that you can create in a Visual Basic .NET based application and how you can make them visible to unmanaged client applications. It also provides an overview of .NET Remoting for component communication.
In Visual Basic .NET, you can create several types of components that are accessible from both managed client applications (those built on the services of the Microsoft .NET Framework common language runtime) and unmanaged client applications (for example, client applications created in Visual Basic 6.0). After you complete this lesson, you will be able to:
n n n n
Describe the types of components that you can create in Visual B asic .NET. Use modules and classes as components. Use Visual Basic .NET based components in unmanaged environments. Explain the key concepts of .NET Remoting.
Types of Components
Topic Objective
To explain the different types of components that you can create in Visual Basic .NET.
n n n n n n
Lead-in
You can create several types of components in a Visual Basic .NET based application.
In Visual Basic .NET, you can create several different types of components, including: Delivery Tip
n n n n n n
Point out that this module focuses on how to create and use component classes, serviced components, and user controls. The other component types are mentioned for reference purposes.
Structures
You can use structures as components by declaring them as public when you define them. Structures support many features of classes, including properties, methods, and events, but are value types, so memory management is handled more efficiently. Structures do not support inheritance.
Modules
You can use modules as components by declaring them as public when you define them. Declaring modules as public allows you to create code libraries that contain routines that are useful to multiple applications. You can also use modules to create reusable functions that do not apply to a particular component, class, or structure. If you have used the GlobalMultiUse or GlobalSingleUse classes in previous versions of Visual Basic, the concept of a code library is not new to you. These classes provide the same functionality in Visual Basic .NET; the client code does not need to qualify these classes by the class name to call the functions.
Classes
You can use classes as components by declaring them as public within an assembly. You can use public classes from any .NET-based client application by adding a reference to the component assembly. You can extend the functionality of classes through mechanisms such as properties, methods, and events. Classes are also extensible through inheritance, which allows applications to reuse existing logic from these components.
Component Classes
A class becomes a component when it conforms to a standard for component interaction. This standard is provided through the IComponent interface. Any class that implements the IComponent interface is a component. Component classes allow you to open your class in a visual designer, and they allow your class to be sited onto other visual designers.
Serviced Components
Serviced components are derived directly or indirectly from the System.EnterpriseServices.ServicedComponent class. Classes configured in this manner are hosted by a Component Services application and can automatically use the services provided by Component Services.
User Controls
User controls are components that are created by a developer to be contained within Windows Forms or Web Forms. Each user control has its own set of properties, methods, and events that make it suitable for a particular purpose. You can manipulate user controls in the Windows Forms and Web Forms designers and write code to add user controls dynamically at run time, just as you can for the controls provided as part of the .NET Framework. Note In this module, you will learn how to create and use component classes, serviced components, and user controls. For more information about structures, modules, and classes, see Module 5, Object-Oriented Programming in Visual Basic .NET,in Course 2373A, Programming with Microsoft Visual Basic .NET (Prerelease).
Declare the Module As Public Reference and Import the Assembly into Client Code
Public Module MyMathFunctions Public Module MyMathFunctions Public Function Square(ByVal lng As Integer) As Long Public Function Square(ByVal lng As Integer) As Long Return (lng ** lng) Return (lng lng) End Function End Function ... ... End Module End Module 'Client code 'Client code Imports MyAssembly Imports MyAssembly ... ... Dim xx As Long == Square(20) Dim As Long Square(20)
In Visual Basic .NET, you can use modules as components outside of the assembly in which they are defined. To make this possible, declare the module as public when you define it. You then need to create a reference in the client assembly to the component assembly and use the Imports statement to allow access to the module methods. The following example shows how to create a public module named MyM athFunctions that defines the function Square. This module is defined within the MyAssembly assembly. The module can then be used as a component in client code, as shown in the second part of the example.
Public Module MyMathFunctions Public Function Square(ByVal lng As Long) As Long Return (lng * lng) End Function ... End Module 'Client code Imports MyAssembly ... Dim x As Long = Square(20)
Note For more information about assemblies, see Module 10, Deploying Applications, in Course 2373A, Programming with Microsoft Visual Basic .NET (Prerelease). For the purposes of this module, you can think of them as similar to Visual Basic 6.0 Microsoft ActiveX dynamic -link libraries (DLLs).
n n
Declare the Class As Public Reference and Import the Assembly into Client Code
Public Class Account Public Class Account Public Sub Debit(ByVal AccountId As Long, Amount As Double) Public Sub Debit(ByVal AccountId As Long, Amount As Double) 'Perform debit action 'Perform debit action End Sub End Sub Public Sub Credit(ByVal AccountId As Long, Amount As Double) Public Sub Credit(ByVal AccountId As Long, Amount As Double) 'Perform credit action 'Perform credit action End Sub End Sub End Class End Class 'Client code 'Client code Imports MyAssembly Imports MyAssembly Dim xx As New Account( )) Dim As New Account( x.Debit(1021, 1000) x.Debit(1021, 1000)
You can use classes as components outside of the assembly in which they are defined by marking the class as public. You then reference the component assembly from the client assembly, and use the Imports statement to allow direct access to the class. The following example shows how to create a public class called Account that defines the Debit and Credit methods. This class is defined in the MyAssembly assembly. A separate client assembly then references the assembly, and the class can then be used to created object instances.
Public Class Account Public Sub Debit(ByVal AccountId As Long, Amount As Double) 'Perform debit action End Sub Public Sub Credit(ByVal AccountId As Long, Amount As Double) 'Perform credit action End Sub End Class 'Client code Imports MyAssembly Dim x As New Account( ) x.Debit(1021, 1000)
Generate a strong name Select Register for COM Interop in Build options
Lead-in
You can use COM to make all Visual Basic .NET components accessible from unmanaged clients, if you follow some simple steps.
Define and implement interfaces Use the ClassInterface attribute with AutoDual value Use the COMClass attribute
Delivery Tip
Remind students that assemblies and strong names will be covered in Module 10, Deploying Applications, in Course 2373A, Programming with Microsoft Visual Basic .NET (Prerelease).
You can create Visual Basic .NET components that can be used by unmanaged client applications. This interoperability allows you to use Component Services features such as object pooling and transactions. In order to expose your components to COM and Component Services, you must set specific assembly properties and create your classes appropriately.
Defining a public interface. Using the ClassInterface attribute. Using the COMClass attribute.
10
Client AppDomain
Marshal By Value
Client Code
Server AppDomain
Lead-in
The .NET Framework provides several services that are used in remoting.
Formatter
Server Proxy
Channel
Marshal By Reference
Client Code
Formatter
Server Object Copy
Channel
Channel
Remoting Boundary
Previous versions of Visual Basic use COM and the distributed version of COM (DCOM) to communicate with components in different processes or on different computers. Visual Basic .NET uses .NET Remoting to allow communication between client and server applications across application domains. Delivery Tip The .NET Framework provides several services that are used in remoting:
n
Point out that this topic only presents an overview. Module 13, Remoting and Web Services, in Course 2349A, Programming the .NET Framework with C# (Prerelease), covers this topic in more depth.
Communication channels are responsible for transporting messages to and from remote applications by using either a binary format over a Transmission Control Protocol (TCP) channel or Extensible Markup Language (XML) over a Hypertext Transfer Protocol (HTTP) channel. Formatters that encode and decode messages before they are transported by the channel. Proxies that forward remote method calls to the proper object. Remote object activation and lifetime support for marshal-by-reference objects that execute on the server. Marshal-by-value objects that are copied by the .NET Framework into the process space on the client to reduce cross-process or cross-computer round trips.
n n
.NET Remoting Note For more information about .NET Remoting, see Technical Overview in the Microsoft Visual Studio .NET documentation.
11
Hosting Components in Component Services Using Transactions Using Object Pooling Using Constructor Strings Using Security Using Other Component Services Configuring Assemblies for Component Services
Lead-in
This lesson examines .NET components that are hosted by Component Services.
Describe the requirements for hosting .NET-based components in a Component Services application. Enable transaction processing in your components. Use object pooling to improve performance for objects that need extra resources. Use security attributes to specify how components interact with Component Services security. Add constructors to control how a component is initialized. Explain how to use other Component Services, such as Just-In-Time activation, from Visual Basic .NET components. Set assembly-level attributes to improve the installation of your application.
n n
n n
12
Lead-in
To enable components to be hosted in Component Services, the .NET Framework provides several items that you need to include in your assembly and classes.
You must add a project reference to the System.EnterpriseServices namespace if you want to host a Visual Basic .NET component in a Component Services application. This namespace provides the main classes, interfaces, and attributes for communicating with Component Services. The System.EnterpriseServices namespace provides the following features.
Feature ContextUtil class Usage Use this class to participate in transactions and to interact with security information. The functionality of this class is similar to the functionality of the ObjectContext class in Visual Basic 6.0. ServicedComponent All component classes that need to be hosted within a Component class Services application must inherit this class. This class defines the base type for all context bound types and implements methods similar to those found in the IObjectControl interface used in Visual Basic 6.0 based Component Services applications. Assembly, class, and You can define several assembly attributes for Component method attributes Services interrogation in the AssemblyInfo.vb file. These values are used to set the application name and description and other values when the application is installed as a Component Services application. Several class and method attributes are also defined by the System.EnterpriseServices namespace, including TransactionAttribute, AutoCompleteAttribute, ObjectPoolingAttribute , and ConstructionEnablesAttribute .
Note The Attribute part of an attribute name is optional, so, for example, you can use either AutoComplete or AutoCompleteAttribute in your code.
13
Using Transactions
Topic Objective
To examine how components can utilize Component Services transactions.
n
Transaction Attribute Specifies How a Class Participates in Transactions ContextUtil Class Provides Transaction Voting AutoComplete Attribute Avoids Using the SetAbort, SetComplete, and ContextUtil Methods
Lead-in
Various objects and attributes enable Visual Basic .NET components to use Component Services transactions.
n n
<Transaction(TransactionOption.Required)> Public Class Account <Transaction(TransactionOption.Required)> Public Class Account Inherits ServicedComponent Inherits ServicedComponent Public Sub Debit(...) Public Sub Debit(...) 'Perform debit action 'Perform debit action ContextUtil.SetComplete( ) ContextUtil.SetComplete( ) End Sub End Sub <AutoComplete( )> Public Sub Credit(...) <AutoComplete( )> Public Sub Credit(...) 'Perform credit action 'Perform credit action 'No SetComplete because AutoComplete is on 'No SetComplete because AutoComplete is on End Sub End Sub End Class End Class
Transactions are often required to maintain data integrity and to synchronize updates to data in multiple data sources. You can enable transaction processing in serviced components by including the appropriate attributes and classes in your component code.
RequiresNew Supported
14
SetComplete
EnableCommit
15
Processing Transactions
To avoid using the SetAbort and SetComplete methods of ContextUtil, you can set the AutoComplete attribute of specific methods of the component. If no exceptions occur during the method execution, the object behaves as if SetComplete has been called. If exceptions do occur, the object behaves as if SetAbort has been called.
16
Object Pooling Allows Objects to Be Created in Advance ObjectPooling Attribute Specifies MinPoolSize and MaxPoolSize ServicedComponent Provides CanBePooled Method
Lead-in
Various attributes and interfaces enable Visual Basic .NET components to use object pooling.
<ObjectPooling(Enabled:=True, MinPoolSize:=5, __ <ObjectPooling(Enabled:=True, MinPoolSize:=5, MaxPoolSize:=50)> _ MaxPoolSize:=50)> _ Public Class Account Public Class Account Inherits ServicedComponent Inherits ServicedComponent ... ... Public Overrides Function CanBePooled( ) As Boolean Public Overrides Function CanBePooled( ) As Boolean Return True Return True End Function End Function End Class End Class
In Visual Basic .NET, you use the ObjectPooling attribute and the ServicedComponent base class to create serviced components that use object pooling.
17
MinPoolSize To set the minimum number of objects to be created in advance in the pool, use the MinPoolSize argument.
MaxPoolSize To set the maximum number of objects that can be created in the pool, use the MaxPoolSize argument. If no objects are available in the pool when a request is received, the pool can create another object instance if this preset maximum number of objects has not already been reached. If the maximum number of objects have already been created and are currently unavailable, requests will begin queuing for the next available object.
True If your component supports object pooling and can safely be returned to the pool, the CanBePooled method should return True.
False If your component does not support object pooling, or if the current instance cannot be returned to the pool, the CanBePooled method should return False.
Note If object pooling is disabled for a component, the CanBePooled method will not be executed.
18
19
Lead-in
Specify the ConstructionEnables Attribute to Indicate a Construction String Is Required Override the Construct Method to Retrieve Information
Component Services provides constructor strings to serviced components that are accessible in Visual Basic .NET components through the .NET Framework.
<ConstructionEnables(True)>Public Class Account <ConstructionEnables(True)>Public Class Account Inherits ServicedComponent Inherits ServicedComponent Public Overrides Sub Construct(ByVal s As String) Public Overrides Sub Construct(ByVal s As String) 'Called after class constructor 'Called after class constructor 'Use passed in string 'Use passed in string End Sub End Sub End Class End Class
You can use a constructor string to control how serviced components are initialized. This allows you to specify any initial information the object needs, such as a database connection string, by using the Component Services management console. You can use the ConstructionEnables attribute to enable this process in a serviced component. Your Visual Basic .NET component can then receive this constructor information because the inherited ServicedComponent class provides the overridable Construct method.
20
Using Security
Topic Objective
To explain how Component Services security is accessible in Visual Basic .NET components.
Security Configuration Attributes Enable Security and Role Configuration SecurityCallContext Class Provides Role Checking and Caller Information
Lead-in
Component Services provide security information that Visual Basic .NET components can use.
<ComponentAccessControl(True), SecurityRole("Manager")> __ <ComponentAccessControl(True), SecurityRole("Manager")> Public Class Account Public Class Account Inherits ServicedComponent Inherits ServicedComponent Public Function GetDetails( ) As String Public Function GetDetails( ) As String With SecurityCallContext.CurrentCall With SecurityCallContext.CurrentCall If .IsCallerInRole("Manager") Then If .IsCallerInRole("Manager") Then Return .OriginalCaller.AccountName Return .OriginalCaller.AccountName End If End If End With End With End Function End Function End Class End Class
When working with serviced components, you can use pre-defined attributes and objects to configure and test security options.
21
22
The following example shows how use SecurityCallContext to determine whether security is enabled, check whether a caller is in the Manager role, and return the AccountName string from the OriginalCaller property, which is a SecurityIdentity instance.
<ComponentAccessControl(True), SecurityRole("Manager")> _ Public Class Account Inherits ServicedComponent Public Function GetDetails( ) As String If ContextUtil.IsSecurityEnabled Then With SecurityCallContext.CurrentCall If .IsCallerInRole("Manager") Then Return .OriginalCaller.AccountName End If End With End If End Function End Class
23
Lead-in
Component Services provides several other services that you can use in Visual Basic .NET components.
Component Services provides a series of other services that you can use from Visual Basic .NET components.
Just-in-Time Activation
When just-in-time (JIT) activation is enabled, an object is automatically instantiated when a method is called on a serviced component (activation), and then automatically deactivated when the method is complete (deactivation). When this optio n is enabled, an object does not maintain state across method calls, and this increases the performance and scalability of the application. You can override the Activate and Deactivate methods inherited from the ServicedComponent class to perform custom functionality during JIT. If object pooling is enabled, the activation occurs when an existing object has been taken from the pool, and the deactivation occurs when the object is placed back in the pool. JIT is automatically enabled if a component is transactional, and it cannot be disabled. You can manually enable or disable JIT for non-transactional components by using the JustInTimeActivation attribute.
Queued Components
Queued components provide asynchronous communication. This allows client applications to send requests to queued components without waiting for a response. The requests are recordedand sent to the server, where they are queued until the application is ready to use the requests. These requests are then played backto the application as if they were being sent from a regular client. You can mark an application for queuing by using the assembly-level ApplicationQueuing attribute. Mark individual components with the InterfaceQueuing attribute.
24
Shared Properties
You can use the Shared Property Manger (SPM) components to share information among multiple objects within the same application process. Use the SPM components as you use them from components created in Visual Basic 6.0.
Synchronization
Distributed applications can receive simultaneous calls from multiple clients. Managing these simultaneous requests involves complex program logic to ensure that resources are accessed safely and correctly. Component Services provides this service automatically to components that use transactions. You can also use the Synchronization attribute to specify this behavior.
25
Lead-in
Setting assembly-level Component Services attributes helps define how your application will behave when you deploy it under Component Services.
You can specify some assembly level attributes that provide information when your assembly is installed as a Component Services application. The information is stored in the AssemblyInfo.vb file that is part of your Visual Basic .NET project.
Assembly attribute ApplicationName Usage If you use this attribute to specify the name of the application, a Component Services application with the same name when your assembly is deployed and installed. Use this attribute to set the Component Services application description value when the assembly is deployed and installed. Use this attribute to specify whether you want to implement your Component Services application as either a library or a server application. The acceptable values for this attribute are ActivationOption.Server or ActivationOption.Library.
Description
ApplicationActivation
26
Manual registration You can use the Regsvcs.exe utility to manually register your assembly. This utility uses the information provided by your assembly attributes so that the Component Services application can be created with the correct default information. The basic syntax for using Regsvcs.exe is shown in the following example:
.NET Framework Install path/Regsvcs.exe myApplication.dll
Automatic registration If you do not register your application manually, registration will automatically occur when a client application attempts to create an instance of a managed class that inherits from the ServicedComponent class. All of the ServicedComponent classes within your assembly will then be registered as part of the Component Services application. This is known as Lazy Registration.
27
Lead-in
Delivery Tip
The step-by-step instructions for this demonstration are in the instructor notes for this module.
In this demonstration, you will learn how to create a serviced component that uses object pooling and how to call the component from a managed client.
28
Objectives
After completing this lab, you will be able to:
n n
Prerequisites
Before working on this lab, you must be familiar with creating and using components in MTS or Component Services.
Scenario
In this lab, you will create a serviced component based on a preexisting class. The class contains a single method that customers use to logon. You will register this assembly with Component Services and create a test harness application that references and tests your component. The test harness will use a preexisting form that allows you to enter a customer e-mail address and s password to retrieve the customer details by using the component.
29
30
31
3. Execute the following command to register the assembly and create the serviced component application:
" %Windir%\Microsoft.NET\Framework \v1.0.2914\Regsvcs.exe"
CustomerComponent.dll
32
4. Confirm that the assembly-level attributes that you specified in your project have been set in the application. 5. Close the Customers Properties dialog box.
33
3. Call the LogOn method of the cust object, passing in the following values.
Parameter Email Password Value txtEmail.Text txtPassword.Text
4. Use the ds Dataset object to store the value returned from the LogOn method.
34
3. Click Logon, and confirm that a record is successfully retrieved from the component. 4. Click Close to quit the test harness application. 5. Quit Visual Studio .NET.
35
Lead-in
36
se Ba ss System.ComponentModel.Component Cla
d Component Classes ve eri ses Predefined Classes D as Cl Custom Classes
IComponent Interface
In addition to supporting classes and structures, the System namespace provides a library of components designed to make component development easy. When you create a component class based on the ComponentModel.Component base class, you automatically inherit the basic architecture for your class.
IComponent Interface
The IComponent interface allows you to create custom components or to configure existing components such as the MessageQueue or Timer components within the visual designer for your component. After you place any existing components on your component (siting), you can access them in your component code in the same way as you can when they are placed in the component tray of a Windows Form.
37
38
Perform any initialization in constructor Override Dispose method Use Server Explorer or Toolbox items Properties, methods, and events
The procedure for creating a component class with Visual Basic .NET is similar to the procedure for creating standard classes, but there are a few extra steps. Delivery Tip 1. Inherit the System.ComponentModel.Component class. The Component Class template item contains the required code to inherit the System.ComponentModel.Component class, including the constructor code required to add your component class to a container. Add any initialization code for your component class as part of the construction process by placing code in the prewritten Sub New method. You can override the Dispose method of the inherited Component class to free any resources before the instance of your component is destroyed. 2. Add any sited components. If your component class requires other components in order to fulfill its purpose, you can add them within the Design view by dragging them from the Toolbox or Server Explorer to your component class. These components can then be programmatically accessed from within the code for your component class. 3. Create required functionality. Your component class can provide public properties, methods, and events to allow the user of your component to interact with it at both design time and run tim e. 4. Build the assembly. Building the assembly enables other managed clients to make a reference to your component.
Point out the sample code in the student notes, but explain that a demonstration immediately follows this topic.
39
The following example shows how to create a component class that is derived from the System.ComponentModel.Component class. It extends the functionality of the standard Timer class by defining additional properties and events.
Public Class Hourglass Inherits System.ComponentModel.Component Public Event Finished( ) Private WithEvents localTimer As System.Timers.Timer Public Sub New( ) MyBase.New( ) 'This call is required by the Component Designer. InitializeComponent( ) 'Initialize the timer for 1 minute (60000 milliseconds) localTimer = New System.Timers.Timer( ) localTimer.Enabled = False localTimer.Interval = 60000 End Sub Public Property Enabled( ) As Boolean Get Return localTimer.Enabled End Get Set(ByVal Value As Boolean) localTimer.Enabled = Value End Set End Property Private Sub localTimer_Tick(...) Handles localTimer.Tick 'Raise the finished event after localtimer_Tick is raised RaiseEvent Finished( ) End Sub Public Overloads Overrides Sub Dispose( ) 'Disable the localTimer object localTimer.Enabled = False localTimer.Dispose( ) MyBase.Dispose( ) End Sub End Class
Delivery Tip
Point out that inheriting from the Timer class would also produce a similar component.
The component behaves as an hourglass that raises a Finished event one minute after it is enabled. The component can be turned on by using the Enabled property at design time or run time. The localTimer is initialized as part of the Sub New constructor and set for a timer interval of 60,000 milliseconds, or one minute. The Dispose method is overridden to ensure that the localTimer object is safely disposed of.
40
Lead-in
This demonstration shows how to create a stopwatch component class and use it from another application.
Delivery Tip
The step-by-step instructions for this demonstration are in the instructor notes for this module.
In this demonstration, you will learn how to create a component class that can be used by another assembly.
41
Inheriting from the UserControl Class Inheriting from a Windows Forms Control Providing Control Attributes
Lead-in
This lesson examines how to create Windows Forms controls in Visual Basic .NET.
In previous versions of Visual Basic, you can create ActiveX controls that can be reused by different client applications. In Visual Basic. NET, you can also use inheritance to create controls. After completing this lesson, you will be able to:
n n n
Create a control based on the System.Windows.Forms.UserControl class. Create a control based on an existing Windows Forms control. Add attributes to your controls that enable advanced design-time functionality.
42
n n n
Inherit from System.Windows.Forms.UserControl Add Required Controls to Designer Add Properties and Methods That Correspond to Those of Constituent Controls Add Any Additional Properties and Methods No InitProperties, ReadProperties, or WriteProperties
l
Lead-in
In Visual Basic .NET, you can inherit from the UserControl class to create the same type of user controls that you can create in Visual Basic 6.0.
n n
In previous versions of Visual Basic, you can create a unique new control by placing one or more existing controls onto a UserControl designer. You can then create custom properties, methods, and events to set and retrieve values for the contained controls. This type of control is useful when several forms require the same layout of controls, such as forms for addresses or contact details.
Place as many controls on the designer as you need to in order to create your own user control. Access these controls within your user control class, because they are declared as private variables. Add your own properties and methods that correspond to the properties and methods of the constituent controls. Add public properties, methods, and events in exactly the same way that you do for a regular class.
43
Example
The following example shows how to create a simple user control that contains a label and a text box:
Public Class LabelAndTextControl Inherits System.Windows.Forms.UserControl Public Property TextBoxText( ) As String Get Return TextBox1.Text End Get Set(ByVal Value As String) TextBox1.Text = Value End Set End Property Public Property LabelText( ) As String Get Return Label1.Text End Get Set(ByVal Value As String) Label1.Text = Value End Set End Property ... 'Windows Form Designer generated code End Class
The TextBox1 and Label1 controls are privately declared variables within the user control that are only accessible using the public properties TextBoxText and LabelText.
44
n n
Allows Enhanced Version of a Single Control Inherit from Any System.Windows.Forms Control
Lead-in
Inheritance makes it easy for you to enhance an existing control in Visual Basic .NET.
Public Class MyTextBox Public Class MyTextBox Inherits System.Windows.Forms. TextBox Inherits System.Windows.Forms.TextBox Private strData As String Private strData As String Public Property HiddenData( ) As String Public Property HiddenData( ) As String Get Get Return strData Return strData End Get End Get Set(ByVal Value As String) Set(ByVal Value As String) strData == Value strData Value End Set End Set End Property End Property ... ... End Class End Class
In previous versions of Visual Basic, you can create enhanced versions of an existing control by placing an instance of the control on the UserControl designer. You can then create public properties, methods, and events that correspond to the equivalent items of the constituent control, adding any custom items to create your enhanced behavior. In Visual Basic .NET, you can create a control that inherits from any System.Windows.Forms class, such as the TextBox or Label class. Because this approach uses inheritance, there is no need to create public properties, methods, and events that map to the constituent control. This greatly reduces the amount of code required. You only need to create any extra functionality, as described for user controls in the previous topic. The following example shows how to create a control that inherits from SystemWindows.Forms and adds a public property:
Public Class MyTextBox Inherits System.Windows.Forms.TextBox Private strData As String Public Property HiddenData( ) As String Get Return strData End Get Set(ByVal Value As String) strData = Value End Set End Property ... End Class
45
This code creates a new control that inherits all of the TextBox class functionality and adds a property called HiddenData. Note For some existing controls, you can create a new graphical front end by overriding the OnPaint method of the base class. However, some controls, such as the TextBox control, are painted directly by Windows and cannot be overridden.
46
System.ComponentModel Provides Control Attributes Class Level DefaultProperty, DefaultEvent, ToolboxBitmap Property Level Category, Description, DefaultValue
Imports System.ComponentModel Imports System.ComponentModel <ToolboxBitmap ("C:\TXTICON.BMP"), DefaultEvent("Click")> __ <ToolboxBitmap("C:\TXTICON.BMP"), DefaultEvent("Click")> Public Class MyTextBox Public Class MyTextBox Inherits System.Windows.Forms. UserControl Inherits System.Windows.Forms.UserControl <Category("Appearance"), _ <Category("Appearance"), _ Description("Stores extra data"), __ Description("Stores extra data"), DefaultValue("Empty")> _ DefaultValue("Empty")> _ Public Property HiddenData( ) As String Public Property HiddenData( ) As String ... ... End Property End Property ... ... End Class End Class
In previous versions of Visual Basic, you can use the Procedure Attributes dialog box to set control attributes, such as property descriptions and their categories, which can be viewed in the Object Browser. You can supply similar information in Visual Basic .NET by using the attributes provided by the System.ComponentModel namespace.
47
48
Lead-in
In this demonstration, you will learn how to create a control based on the Windows Forms TextBox control.
Delivery Tip
The step-by-step instructions for this demonstration are in the instructor notes for this module.
In this demonstration, you will learn how to create a Windows Forms user control based on the existing TextBox.
49
Lead-in
This lesson examines how to create Web Forms user controls in Visual Basic .NET.
In Visual Basic .NET, you can create controls for use within ASP .NET Web Forms. After completing this lesson, you will be able to:
n
Create a Web Forms user control based on other controls in the System.Web.UI.UserControl class. Use a Web Forms user control within a Web Form.
50
1. Add a Web User Control to an ASP.NET Web Project 2. Use the Toolbox to Drag Existing Controls to the Web User Control Designer 3. Add Properties and Methods 4. Save the .ascx File 5. Drag the .ascx File from Solution Explorer to the Web Forms Designer 6. Create Web Form Code as Usual
Lead-in
You can create your own Web user controls by extending the existing Web server controls provided by ASP .NET.
Creating your own Web user control allows you to extend the controls provided with ASP .NET. You can extend a single control with added features or create a new control that is a combination of existing controls. To create your own Web user control: 1. Add a Web user control to your ASP .NET Web project. 2. Use the Toolbox to drag-and-drop existing Web server controls to the Web user control designer. 3. Add properties and methods in the code-behind file. 4. Save the .ascx Web user control file. To use your Web user control: 1. Open your Web Form. 2. Drag the .ascx file from Solution Explorer to the Web Forms Designer. 3. Create any Web Form code that accesses the Web user control, as you would for existing Web server controls. 4. Test your control by running your application and displaying the Web Form.
51
Public MustInherit Class SimpleControl Public MustInherit Class SimpleControl Inherits System.Web.UI.UserControl Inherits System.Web.UI.UserControl Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox Public Property TextValue( ) As String Public Property TextValue( ) As String Get Get Return TextBox1.Text Return TextBox1.Text End Get End Get Set(ByVal Value As String) Set(ByVal Value As String) TextBox1.Text = Value TextBox1.Text = Value End Set End Set End Property End Property End Class End Class
To create a Web user control, you need to create: 1. The graphical layout of the controls in the .ascx file. 2. The code that executes in the.ascx.vb code-behind file. The following example shows how to create a Web user control based on the existing TextBox control while inheriting from the UserControl class. It also provides a custom property for setting the TextBox1.Text value. The following code is located in the Web user control .ascx file:
<%@ Control Language="vb" AutoEventWireup="false" Codebehind="SimpleControl.ascx.vb" Inherits="MyApp.SimpleControl"%> <asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
The example code shows the similarity between Web Forms and Web user control code, the main difference being the @ Control directive and the lack of any <html>, <body>, or <form> tags.
52
The SimpleControl class is similar to most classes in that it allows public access to private members of the class. However, note that it is through inheriting the UserControl class that the Web user control functionality is provided.
53
Lead-in
This demonstration shows how to create a simple Web Forms user control that uses a Label and a TextBox as its constituent controls.
Delivery Tip
The step-by-step instructions for this demonstration are in the instructor notes for this module.
In this demonstration, you will learn how to create a simple Web Forms user control that contains a Label and a TextBox as its constituent controls.
54
Objectives
After completing this lab, you will be able to:
n n
Create a Web Forms user control. Use a Web Forms user control on a Web Form.
Prerequisites
Before working on this lab, you must be familiar with creating Web Form applications.
Scenario
In this lab, you will create a Web Forms user control that requests logon information for a customer. The control will retrieve the customer information by means of the serviced component that you created in the previous lab. You will then use this control on a Web Form and test the control.
55
Display RequiredFieldValidator (ID) ErrorMessage ControlToValidate Display Label TextBox (ID) Text (ID) TextMode
56
Module 9: Developing Components in Visual Basic .NET (continued ) Control RequiredFieldValidator Property name (ID) ErrorMessage ControlToValidate Display Label (ID) Text ForeColor Visible Button (ID) Text Property value rfvPassword Please enter a password txtPassword Dynamic lblNotFound Not found message Red False btnSubmit Submit
57
3. Create a Click event handler for the btnSubmit event. In this method, set the Visible property of the lblNotFound label to False, and raise the SubmitPressed event, passing the following parameters:
Parameter Email Password Value txtEmail.Text txtPassword.Text
4. Create a DisplayMessage subroutine that accepts a single string argument called Message. Within the subroutine, set the following values for the lblNotFound label.
Control property Text Visible Value Message True
58
59
5. Create an event handler procedure for the SubmitPressed event of Logon1, and add the following code:
Dim ds As DataSet, dr As DataRow Dim cust As ICustomer = New Customer( ) ds = cust.Logon(Email, Password) If ds.Tabl es(0).Rows.Count = 0 Then Logon1.DisplayMessage _ ("No match was found. Please reenter your details.") Else dr = ds.Tables(0).Rows(0) Session("FirstName") = dr("FirstName") Response.Redirect("Welcome.aspx") End If
5. Confirm that an error message is displayed by the user control. 6. Enter the same e-mail address as in step 4, but use the cor rect password of password, and then click Submit. Confirm that the welcome message is displayed and that the customer has been recognized. 7. Quit Microsoft Internet Explorer and Visual Studio .NET.
60
u Threading
Topic Objective
To provide an overview of the topics covered in this lesson.
n n n n n
What Is a Thread? Advantages of Multithreading Creating Threads Using Threading When to Use Threading
Lead-in
Visual Basic .NET allows developers to use the power of threading in a way not previously available in Visual Basic.
Previous versions of Visual Basic have limited threading support. Visual Basic .NET allows developers to use the full power of threads when necessary. When you use threading correctly, you can enhance the performance of your application and make it more interactive. After you complete this lesson, you will be able to:
n n n n
Explain the basic concepts of threading. List the advantages of incorporating multithreading into your applications. Create and use threads by using the System.Threading namespace. Avoid some potential problems in your multithreaded applications.
61
What Is a Thread?
Topic Objective Lead-in
Before examining how Visual Basic .NET enables threading, it is important to understand the basic concepts of threading. To explain the basic concepts of threading.
n
The computer appears to perform multiple tasks at one time Every thread contains its own call stack and storage Thread Scheduler Thread 1 Thread 2 3 1 Thread 2 Thread 3
Process 1 Process 2
CPU
Delivery Tip
The slide associated with this topic is an animated slide. Click the slide to reveal the following lessons, showing the iterative process of the thread scheduler: 1. Thread 1 2. Thread 2 3. Thread 3 4. Thread 1 5. Thread 2 6. Thread 3
An application running on a computer is known as a process. Each process gets work done by using one or more threads. The thread is the unit of execution that is processed by the CPU of the computer.
Threading Process
A CPU can only execute a single thread at any one instant, so a thread scheduler allocates a certain amount of CPU time for each thread to get as much work done as possible before allowing another thread to access the CPU. This scheduling makes a computer appear to perform multiple tasks at once. In reality, the following is what happens: 1. Every thread contains its own call stack and storage for local variables. This information is kept with the thread and passed to the CPU whenever the thread is scheduled for processing. 2. When the time is up, the thread scheduler removes the thread from the CPU and stores the call stack and variable information. The more threads that are running on the system, the less frequently a thread is scheduled to run in the CPU. This is why a computer can appear to be running slowly when you have multiple applications open and functioning at the same time.
62
Threading Types
Different programming languages support different types of threading:
n
Previous versions of Visual Basic support the apartment threading model. This model places some restrictions on the types of applications that these versions are best suited for creating. One of these restrictions is that an object is tied to the thread that it is created on, and cannot be used for object pooling in Component Services. However, this model makes development easy because you do not need to be involved with more complex issues such as synchronization.
Visual Basic .NET supports the free threading model. This model allows you to use multithreading and features such as object pooling or to continue using single threads as you have in applications created with previous versions of Visual Basic.
63
Advantages of Multithreading
Topic Objective
To explain the advantages of multithreading and free threading.
Lead-in
n n n
Use threads for lengthy processing operations, such as using the spelling checker or reformatting pages. These extra threads can then raise events to the main user interface thread to update items such as a status bar. Assign each thread a priority level so that particular threads can run as a higher priority than other lower priority threads. In an application that relies heavily on user interaction, you should run the user interface thread as a higher priority thread.
No Blocking
Blocking occurs because a call to a single-threaded application must wait until any previous call by another client application has been fully satisfied before executing any other code. In server-based applications, blocking will occur if multiple clients make simultaneous requests of a process and only a single thread is available. Multithreaded applications are able to perform actions on different threads simultaneously (through thread scheduling) without waiting for other threads to finish their current execution. This allows multiple clients to be handled by different threads without any blocking in a server-based application.
64
Asynchronous Communication
Asynchronous communication is possible in a multithreaded application because one thread can make a request to another thread. The calling thread can continue with other processing because the request executes on a separate thread. An event can be raised when the second thread finishes executing the requested functionality, informing the first thread that it has completed its work.
No Thread Affinity
Visual Basic .NET uses the free threading model. This model does not restrict you to using an object only on the thread where it was initially created. You can create an object on one thread and then pass it to another thread without difficulty. This improves scalability when used in conjunction with Component Services and object pooling.
65
Creating Threads
Topic Objective Lead-in
The .NET Framework provides the System.Threading.Thread class, which allows you to create multiple threads.
n
Constructor specifies delegate method Methods provide control of thread processing Properties provide state and priority information Allow public access to class variables Raise an event when finished
The .NET Framework provides a simple way to create and work with multiple threads.
Use the Thread class to create multiple threads within a Visual Basic .NET based application.
66
Threading Methods
The Thread class also provides several methods to control the processing of a thread.
Method Start Abort Sleep Purpose Begins execution of the method delegate declared in the thread constructor. Explicitly terminates an executing thread. Pauses a thread. Specifies the number of milliseconds as the only parameter. If you pass zero as the parameter, the thread gives up the remainder of its current time slice. This is similar to DoEvents in previous versions of Visual Basic. Temporarily halts execution of a thread. Reactivates a suspended thread.
Suspend Resume
Threading Properties
The Thread class provides properties to retrieve information about the thread state and to manipulate the thread priority.
Property ThreadState Priority Purpose Use the ThreadState property to determine the current state of a thread, such as Running , Suspended, or Aborted. Modify the priority of a thread by setting its Priority property by using the ThreadPriority enumeration. The enumeration provides the following values: AboveNormal, BelowNormal, Highest, Lowest, and Normal.
Warning If you set thread priorities to a value of Highest, this may affect vital system processes by depriving them of CPU cycles. Use this setting with caution.
67
You cannot specify a method delegate that accepts arguments in the thread constructor. If your procedure requires information to perform its required action, you can:
n n
Use classes to provide methods that perform operations on local data. Use public properties or variables to supply the local data.
To use classes to supply parameters, you must create an instance of the class before calling the thread constructor. Use the AddressOf operator to pass a reference to the method of the class as the constructor parameter. You can then use the properties or public variables to supply any data required by the method. When the worker method finishes its execution, you can raise an event to inform the calling thread that the operation is completed.
68
Using Threading
Topic Objective Lead-in
Lets take a look at a simple threading example. To explain a simple example of threading.
Class Calculate Class Calculate Public iValue As Integer Public iValue As Integer Public Event Complete(ByVal Result As Integer) Public Event Complete(ByVal Result As Integer) Public Sub LongCalculation( ) Public Sub LongCalculation( ) 'Perform a long calculation based on iValue 'Perform a long calculation based on iValue ... ... RaiseEvent Complete(iResult) 'Raise event to signal finish RaiseEvent Complete(iResult) 'Raise event to signal finish End Sub End Sub End Class End Class Sub Test( ) Sub Test( ) Dim calc As New Calculate( ) Dim calc As New Calculate( ) Dim th As New Threading.Thread(AddressOf calc.LongCalculation) Dim th As New Threading.Thread(AddressOf calc.LongCalculation) calc.iValue = 10 calc.iValue = 10 AddHandler calc.Complete, AddressOf CalcResult AddHandler calc.Complete, AddressOf CalcResult th.Start( ) th.Start( ) End Sub End Sub Sub CalcResult (ByVal Result As Integer) Sub CalcResult(ByVal Result As Integer) ... ... End Sub End Sub
This topic shows how to prepare a class for threading, create a thread, start the thread, and perform calculations on the new thread.
The class provides a LongCalculation worker function, which will be executed on a separate thread. The worker function uses information stored in the public iValue integer variable to calculate its result. The Calculate class provides a Complete event to notify the calling thread that the calculation is finished.
69
The Test subroutine instantiates a Calculate object and specifies the LongCalculation delegate in the Thread constructor. A value is assigned to the iValue variable for use by the worker function. An event handler is created to detect completion of the calculation. The Start method is called on the separate thread to begin the processing of the calculation.
n n n
70
Using more threads requires more system resources Prevent two threads from accessing shared data simultaneously Use SyncLock statement to block sections of code
Lead-in
Sub Worker( ) Sub Worker( ) SyncLock(theData) 'Lock this object variable SyncLock(theData) 'Lock this object variable theData.id == iValue theData.id iValue 'Perform some lengthy action 'Perform some lengthy action iValue == theData.id iValue theData.id End SyncLock 'Unlock the object variable End SyncLock 'Unlock the object variable End Sub End Sub
Delivery Tip
Point out that incorrect use of threads can have serious consequences.
Using multiple threads is a useful programming concept in enterprise development; however, improper use of threads can cause performance problems, create inconsistent data, and cause other errors.
System Resources
Threads consume memory and other valuable resources, such as CPU processing time. If your application creates multiple threads, it may do so at the expense of other applications or other threads within your own process. The more threads you create, the longer the delay between CPU time slices for each thread. If all applications created an excessive number of threads and used them constantly, the system would spend most of its time swapping threads in and out of the CPU, since the thread scheduler itself requires the CPU to perform the swapping logic.
71
Shared Resources
If multiple threads need to access the same information at the same time, a concurrency problem may arise. Two threads accessing a shared global resource may get inconsistent results back from the resource if other threads have altered the data. The following is an example of a situation in which this can occur:
n
Thread A updates a value on a shared resource such as an integer, setting the value to 10 before performing some lengthy action. Thread B updates the same integer value to 15 during the delay of thread A lengthy action. s When this action is completed, thread A may read the integer value back from the resource whose value is now 15.
72
73
Lead-in
This demonstration shows how to use the SyncLock statement to synchronize a shared resource.
Delivery Tip
The step-by-step instructions for this demonstration are in the instructor notes for this module.
In this demonstration, you will learn how to use the SyncLock statement when using multiple threads in an application created in Visual Basic .NET.
74
Review
Topic Objective
To reinforce module objectives by reviewing key points.
n n n n n n
Components Overview Creating Serviced Components Creating Component Classes Creating Windows Forms Controls Creating Web Forms User Controls Threading
Lead-in
The review questions cover some of the key concepts taught in the module.
1. An unmanaged client application uses a class created in Visual Basic .NET but cannot access any methods of the class. What is the likely cause of this problem, and how would you fix it? The class may have public methods defined without using an interface or any class -level attributes. To solve this problem, create and implement methods in interfaces rather than classes, use the ClassInterface attribute, or use the COMClass attribute.
75
2. Modify the following code to use auto completion of transactions rather than the explicit SetAbort and SetComplete methods.
<Transaction(TransactionOption.Required)> _ Public Class TestClass Public Sub MySub( ) Try 'Perform action ContextUtil.SetComplete( ) Catch ex As Exception ContextUtil.SetAbort( ) Throw ex End Try End Sub End Class <Transaction(TransactionOption.Required)> _ Public Class TestClass <AutoComplete( )>Public Sub MySub( ) 'Perform action End Sub End Class
3. Create assembly attributes so Component Services can automatically create an application named TestComponentsthat runs as server activation. <Assembly: ApplicationName("TestComponents")> <Assembly: ApplicationActivation(ActivationOption.Server )>
4. Why would you use the IComponent interface? The interface enables component classes to site other components and enables the component class to be sited on other components.
76
5. The following code causes a compilation error. Explain what is causing the error and how it could be fixed.
Sub Main( ) Dim t As New Thread(AddressOf MySub) t.Start(10) End Sub Sub MySub(ByVal x As Integer) ... End Sub
The MySub procedure cannot be called directly because it expects an argument and the Start method of a Thread cannot accept parameters. To fix this error, you could create the following code:
Sub Main( ) Dim obj As New ThreadObj( ) Dim t As New Thread(AddressOf obj.MySub) obj.x = 10 t.Start( ) End Sub
Class ThreadObj Public x As Integer Sub MySub( ) ... End Sub End Class