Thread Pool Manager

Throughout this book, you've seen different ways of creating threads in this chapter, we've described the ThreadPool class to make use of the OS's own thread pool for short-lived threads. You can implement a halfway house between the two, however, by creating a class that keeps a pool of a specified number of threads to be supplied to any requesting application. This enables the threads to be managed more easily by your code, and allows for faster thread execution as you might be able to use a...

Manual Synchronization

The third synchronization strategy concerns manual techniques. The .NET Framework provides a classic suite of techniques that give the programmer the ability to create and manage multithreaded applications using a low-level threading API (Application Programming Interface) similar to the WIN32 Threading API. The following table shows some of the classes in the System.Threading namespace that can be used for manual synchronization. The AutoResetEvent class is used to make a thread wait until...

Synchronized Code Regions

The second synchronization strategy concerns synchronizing specific code regions. This section looks at the Monitor and ReaderWriterLock classes. Monitors are used to synchronize sections of code by acquiring a lock with the Monitor.Enter() method and then releasing the lock using the Monitor.Exit() method. The concept of a lock is normally used to explain the Monitor class. One thread gets a lock, while others wait until the lock is released. After the lock is acquired on a code region, you...

Defining the Remoting Class

We'll define a simple remoting class named MonitorTimespan. This class has a parameterized constructor, and a function named GetInfo() that can be called by client applications. Imports System Namespace MyRemoteNamespace Public Class MonitorTimespan Inherits MarshalByRefObject ' Date and time we were created, ' plus an ID for this object Dim mCreationTime As DateTime Dim mID As String ' Parameterized constructor, ' must be called manually by server Public Sub New(ByVal ID As String)...

Implementing the Invert Server Channel Sink

The server sink class itself is different, however. First, it doesn't have to implement IMessageSink. Message sinks are a client-side concept, and they don't appear on the server side at all. Partly because of this, there tends to be more arguments to most of the methods, as they are effectively doubling up on both interfaces. This is at its most spectacular in the ProcessMessage() method, as you'll see soon. However, the class starts off the same as on the client side Public Class...

Thread Sleep and Clock Interrupts

As stated previously, the programs may have yielded execution to another thread so it can wait on some outside resource. However, the resources may not be available the next time the thread is brought back to execute. In fact, it may not be available the next 10 or 20 times a thread is executed. The programmer might want to take this thread out of the execution queue for a long period so that the processor doesn't waste time switching from one thread to another just to realize it has to yield...

Programming the Thread Pool in VB NET

The previous sections of the chapter dealt with theoretical aspects of using thread pools in the .NET Framework. Now it's time to cover the programmatic aspects of creating and using thread pools in .NET applications from a VB .NET perspective. As described in the previous section, the System.Threading namespace contains the ThreadPool class that you can use to create a thread pool in .NET applications. Before we start coding, you need to know three important rules concerning the ThreadPool...

Database Connection Pool

Object pools are common in enterprise software development where instantiation of objects has to be controlled to improve the performance of the application. For example, database connections are expensive objects to be created every time you need to connect to a database. So, instead of wasting resources in instantiating the database connection for every database call, you can pool and reuse some connection objects that you've already created and thus gain a performance advantage by saving the...

Class Employee Site Implements ISite

' A site object associates a container with a component Private mTheContainer As IContainer Private mTheComponent As IComponent ' A site object uses a unique site name for each component in a ' container. Because we're in the EmployeeSite class, we'll use the ' employee's ID as the site name. Private mEmpID As String ' Constructor, to associate a container with a component Public Sub New ByVal TheContainer As IContainer, _ ByVal TheComponent As IComponent mTheContainer TheContainer...

Writing Your Own Thread Safe Wrappers

The general idea of writing your own wrapper comes from the fact that you might not want to make every class in your library thread-safe, as synchronization has performance penalties associated with it. Instead, you can give the application developer a choice of whether to use a synchronized class or not. The application developer might not want to take the risk of a deadlock nor pay the performance penalty of using thread-safe classes in a single-threaded environment instead, he might prefer...

Modifying the Host and Client Applications

We don't need to change any of the code for the ExchangeHost application, but we need to include a copy of ExchangeInterface.dll that we get when the solution is built with the host project. We do, however, have to modify the ExchangeClient code a little. It now uses only the IExchange interface rather than the Exchange class Imports System.Runtime.Remoting Imports ExchangeInterface Dim url As String Dim exchange As IExchange _ Activator.GetObject GetType IExchange , url Dim order As Order...

Exploring the Thread Pool Class

This section explores the various aspects of the ThreadPool class and shows how they can be employed to create thread pools in your .NET applications. The ThreadPool class provides a pool of threads that can be used for the following Processing asynchronous I O calls Waiting on behalf of other threads The following table lists the methods of the ThreadPool class and their functionality. QueueUserWorkItem RegisterWaitForSingleObject This method binds the OS handle to the thread pool. This method...

The Field Info Class

This class provides access to field metadata. Tables 8-7 and 8-8 list some of its most important properties and methods remember that because FieldInfo inherits from MemberInfo, it inherits its members too . Table 8-7. FieldInfo Class Properties Table 8-7. FieldInfo Class Properties Gets a value indicating whether the field is private Gets a value indicating whether the field is public Gets a value indicating whether the field is static Returns a value indicating whether this field has Assembly...

Creating the Client

Before we start building the application, let's take a moment or two to see the UML view of the client form class that is going to contain all the code for the client application see Figure 19-2 . Figure 19-22 UML view of the client form class Figure 19-22 UML view of the client form class The StockClient application simply inherits from the System.Windows.Forms.Form class and contains all the code for the client application, such as the Private member variables and the methods. To create the...

Implementing the Abstract Factory Pattern with Dynamic Assembly Loading in VB NET

Let's examine a situation where you might apply these techniques. A manufacturing company's accounting department wants to create an EDI Electronic Data Interchange invoicing and billing system. EDI systems are electronic connections to send information between the two companies electronically. To keep material costs low, the purchasing department wants as little barrier to switching suppliers as possible. Unfortunately, different vendors all use different systems for communication email, FTP...

End

Next, let's look at the event handler method for the Get Status button. This method reads the account number entered by the user, and calls GetAccountDetails on the remote Bank object. This method returns the requested Account object, and we display its details in a message box. You can probably imagine at least half a dozen things that can go wrong in this sequence of events we'll ignore these potential errors until later in the chapter. Private Sub btnGetStatus_Click ByVal sender As...

Invoke Member Property Value Modification Example

Many IDEs come with integrated property pages that allow you to set initial values and configure object properties at design time. These property pages are interesting because they accommodate any type of object or component and expose their internal properties to be viewed and modified. For example, while developing Windows applications, you probably fill your form with a number of controls. Each time you click on a different control, the property viewer refreshes its contents and exposes all...

Invoke Member Method Invocation Example

Near the start of the chapter, we discussed some very tempting features of dynamic method invocation and suggested its use in a number of situations and scenarios. Now let's see how these features can accent your applications to become more flexible, and easy to extend and implement. We're going to precede this section by modeling a real-world situation, where reflection could prove to be a solution. We'll use a variation on the user input-checking scenario we discussed at the start of the...

Tracing Switches

When you're near to the application deployment phase, you'll probably want to remove all the tracing and debugging messages from the code. However, you don't have to look for every trace instruction and remove it. You can use compilation flags during the application building. From the Visual Studio .NET IDE, right-click on the project name within the Solution Explorer window, and select Properties from the context menu. The following dialog box appears. Now to strip all the Trace and Debug...

Creating the Exchange Objects Class Library

Here are the contents of the ExchangeObjects class library, which contains the implementation of the remote MBR Exchange class and the MBV Order class. Recall that the Exchange class contains the NextOrder method that returns an Order object when called by the client. Imports System.Runtime.Remoting.Lifetime Public Class Exchange Inherits System.MarshalByRefObject Public Sub New Console.WriteLine Exchange Started mNextlndex 1000 End Sub Public Function NextOrder As Order mNextlndex mNextlndex 1...

Manipulating Property Values Using Invoke Member

InvokeMember goes beyond just dynamic method invocation. You can access and modify property values of an object by tweaking the binding flags passed to it during invocation. Because there isn't much difference between a call made to modify a property and a call made to invoke a method, let's hit some code straight away. The following code invokes two properties of an object. The first one is a Private Shared property and we try to get its value by using the Bindings.GetProperty flag Dim FooType...