[ Pobierz całość w formacie PDF ] .Synchronizing Shared ResourcesYou can avoid inconsistent results by locking the resource between the timethat the value is initially set and the time that it is read back.You can use theSyncLock statement to lock a reference type such as a class, interface, module,array, or delegate.The following example defines a shared resource called SharedReference thatexposes an integer variable.The ThreadObj class defines the method that willbe executed by different threads.This method uses the SyncLock statement tolock the shared resource object while it is in use.The module code shows howyou can test this behavior by creating two threads and two worker objects, andthen starting both threads consecutively.72 Module 9: Developing Components in Visual Basic.NETImports System.Threading'Shared dataPublic Class SharedReferencePublic Id As IntegerEnd Class'Class for running on other threadsPublic Class ThreadObjPrivate sr As SharedReferencePrivate Count As Integer'Constructor with reference and IdPublic Sub New(ByRef sharedRef As SharedReference, _ByVal ID As Integer)sr = sharedRefCount = IDEnd Sub'Actual worker methodPublic Sub RunMethod( )SyncLock (sr) 'Lock sr objectsr.Id = Count'Execute lengthy code'sr.Id could have changed without SyncLockCount = sr.IdEnd SyncLock 'Release sr object lockEnd SubEnd ClassModule MainModuleSub Main( )'Create shared data objectDim sr As New SharedReference( )'Create two worker objectsDim worker1 As New ThreadObj(sr, 1)Dim worker2 As New ThreadObj(sr, 2)'Create two threadsDim t1 As New Thread(AddressOf worker1.RunMethod)Dim t2 As New Thread(AddressOf worker2.RunMethod)'Start both threadst1.Start( )t2.Start( )End SubEnd Module Module 9: Developing Components in Visual Basic.NET 73Demonstration: Using the SyncLock StatementTopic ObjectiveTo demonstrate how tosynchronize sharedresources by using theSyncLock statement.Lead-inThis demonstration showshow to use the SyncLockstatement to synchronize ashared resource.In this demonstration, you will learn how to use the SyncLock statement whenDelivery Tipusing multiple threads in an application created in Visual Basic.NET.The step-by-stepinstructions for thisdemonstration are in theinstructor notes for thismodule.74 Module 9: Developing Components in Visual Basic.NETReviewTopic ObjectiveTo reinforce moduleobjectives by reviewing keypoints.Components OverviewLead-inCreating Serviced ComponentsThe review questions coversome of the key conceptsCreating Component Classestaught in the module.Creating Windows Forms ControlsCreating Web Forms User ControlsThreading1.An unmanaged client application uses a class created in Visual Basic.NETbut cannot access any methods of the class.What is the likely cause of thisproblem, and how would you fix it?The class may have public methods defined without using an interfaceor any class -level attributes.To solve this problem, create andimplement methods in interfaces rather than classes, use theClassInterface attribute, or use the COMClass attribute. Module 9: Developing Components in Visual Basic.NET 752.Modify the following code to use auto completion of transactions ratherthan the explicit SetAbort and SetComplete methods._Public Class TestClassPublic Sub MySub( )Try'Perform actionContextUtil.SetComplete( )Catch ex As ExceptionContextUtil.SetAbort( )Throw exEnd TryEnd SubEnd Class_Public Class TestClassPublic Sub MySub( )'Perform actionEnd SubEnd Class3.Create assembly attributes so Component Services can automatically createan application named TestComponents that runs as server activation.4.Why would you use the IComponent interface?The interface enables component classes to site other components andenables the component class to be sited on other components.76 Module 9: Developing Components in Visual Basic.NET5.The following code causes a compilation error.Explain what is causing theerror and how it could be fixed.Sub Main( )Dim t As New Thread(AddressOf MySub)t.Start(10)End SubSub MySub(ByVal x As Integer)
[ Pobierz całość w formacie PDF ] zanotowane.pldoc.pisz.plpdf.pisz.plmikr.xlx.pl
|