Browse DevX
Sign up for e-mail newsletters from DevX


Touring Whidbey's Base Class Library Enhancements : Page 3

The core API set underpinning managed application development in .NET—the Base Class Libraries (BCL)—have received several notable additions in the Whidbey release. Find out performance-based improvements, class-oriented feature additions, and the introduction of previously missing functionality through entirely new classes.

Obtaining Configuration Details
Enhancements to the BCL provide options for retrieving and updating configuration information from managed code. The Configuration class, found in the System.Configuration namespace, provides the ability to easily retrieve machine configuration information. Much of the System.Configuration namespace is contained in the System.dll assembly, but the Configuration class is contained within System.Web.dll. If you want to retrieve connection string information, in particular, from the new <connectionStrings> configuration schema element, new features to the ConfigurationSettings class are an easier route to follow. For example:
   Dim config As Configuration = _
      Configuration.GetMachineConfiguration( _    
   Dim section As ConfigurationSection = _
   Dim node As XmlNode = section.GetRawXml
   ' Simpler route using ConfigurationSettings
   Dim conn As String = _
      ConfigurationSettings.ConnectionStrings. _  
Tracing to the Console
The Trace class provides a standard process for tracing code executing application instrumentation. BCL enhancements include a new ConsoleTraceListener class that joins the existing DefaultTraceListener, EventLogTraceListener and TextWriterTraceListener classes. The ConsoleTraceListener class abstracts the process of sending tracing statements to a console. This simplifies the process of including the console as a destination for trace messages by eliminating the need to write a custom listener to accomplish this goal. In the following code snippet, I'll add a new instance of the ConsoleTraceListener to the Listeners collection:
   Sub SetUpListener()
     Trace.Listeners.Add(New ConsoleTraceListener)
     Trace.WriteLine("Console Listener initiated")
   End Sub
Timing Code Execution
Timing code execution from a non-arbitrary point A to another point B in code has never been a difficult thing to accomplish. On the other hand, the .NET Framework did not simplify this task. The BCL adds a Stopwatch class that provides methods and properties to accurately measure elapsed time.

The Stopwatch class provides all of the intuitive features you expect to find. Call the Start method to instruct the class to begin counting from the current elapsed time value. Do not start an already running Stopwatch, as it has no impact. Call the Stop method to end the current time measurement recording or to establish an interval time and pause the Stopwatch. Do not stop an already stopped Stopwatch, as it has no impact. Call the Start method again to resume time measurement recording. Call the Reset method to clear the cumulative elapsed time.

You can retrieve the elapsed time for a Stopwatch instance by checking the Elapsed, ElapsedTicks, or ElapsedMilliseconds properties. You can call ElapsedTicks to retrieve a raw indicator of elapsed units or use the Frequency field to determine the relationship between ticks and actual elapsed time in direct terms such as milliseconds.

A tick is the smallest unit a Stopwatch measures. A Stopwatch tick is not the same as a System.Ticks value. A System.Ticks value represents a 100-nanosecond span. A Stopwatch tick represents a time interval equal to one second divided by the value of the Frequency field. For example, a high-frequency timer, such as that shown in Figure 3, has a Frequency value in the millions, providing sub-millisecond elapsed time resolution granularity. Support of the hardware and OS is required for the underlying timing mechanism to provide high-resolution. Check IsHighResolution to determine whether the Stopwatch supports high-resolution elapsed time intervals.

Thanks for your registration, follow us on our social networks to keep up-to-date