Support for Timeouts in Regular Expression Evaluations
.NET Framework 4.5 introduces a new constructor for the
System.Text.RegularExpressions.Regex class that accepts a
matchTimeout parameter to establish a timeout for the regular expression resolution. When you specify a value for the new
matchTimeout parameter, if the regular expression engine doesn't resolve the regular expression in the specified time, it will throw a
RegexMatchTimeoutException exception. This way, when you have very complex regular expressions, you have an easy way to establish a timeout and you don't have to use complex workarounds to generate your own timeout mechanisms.
The following code uses the new constructor with a 1000 milliseconds
TimeSpan parameter to establish a 1,000 milliseconds timeout:
var regex = new Regex( @"^(?<proto>\w+)://[^/]+?(?<port>:\d+)?/", RegexOptions.None, TimeSpan.FromMilliseconds(1000));
The read-only property
MatchTimeout provides you the time out interval specified in the aforementioned constructor. If you didn't specify a value, the default value is
Regex.InfiniteMatchTimeout, and therefore, will run with no time out.
Support for Unicode 6.0.0 in Culture-Sensitive Sorting and Casing Rules on Windows 8
.NET Framework 4.5 still includes its own string comparison data that implements Unicode 5.0.0. However, things change when you use .NET Framework 4.5 on Windows 8; it delegates culture-sensitive sorting and casing rules for string comparison to the operating system.
Windows 8 implements Unicode 6.0.0, and therefore, casing, normalization, sorting, and Unicode character information on this operating system conforms to this Unicode version instead of the default Unicode 5.0.0. (If you want more information about Unicode 6.0.0, you can read the details about this major version of the Unicode Standard). You must take that into account whenever you use the
Compare methods with strings and you deploy to Windows 8.
Simple Default Culture Definition for an Application Domain
If you worked with multithreaded applications in previous .NET Framework versions and you had to share the same culture with all the worker threads, you already know some workarounds to do that, such as passing a
CultureInfo instance to each new
.NET Framework 4.5 finally allows you to set both the culture and the UI culture for all threads in an application domain by just assigning a
CultureInfo instance to the following two static properties of the
For example, the following two lines of code set both the culture and the UI culture to
en-US (the code assumes that you import types defined in
CultureInfo.DefaultThreadCurrentCulture = CultureInfo.CreateSpecificCulture("en-US"); CultureInfo.DefaultThreadCurrentUICulture = CultureInfo.CreateSpecificCulture("en-US");
This way, all the threads created after the previously shown code will use
en-US for both the culture and the UI culture. You can easily verify that by checking the values for the following two properties in any thread (and they will have the same value):
Because tasks use underlying threads, the setting also applies to any task you create. This way, you can easily set the desired culture with two simple lines of code and then forget about the issue in your entire application domain.
Internationalized Domain Names in Windows 8 Apps
When you use .NET Framework 4.5 on Windows 8, the
System.Globalization.IdnMapping class supports the 2008 version of the IDNA (short for Internationalized Domain Names in Applications) standard as defined in RFC 5891.
IdnMapping class supports the use of non-ASCII characters for Internet domain names and provides methods to encode and decode domain name labels. When run on Windows 8, the methods handle particular sets of characters by using the 2008 version of the IDNA standard. However, when you execute them on other Windows versions, the methods use the 2003 version of the IDNA standard, as defined in RFC 3490.
If you work with the
IdnMapping class or you are planning to do so, take into account the possible differences when handling particular sets of characters when deploying to Windows 8.
In this first article, I've provided a brief overview of some of the important global improvements introduced in .NET Framework 4.5 that affect the CLR. In the next article, I'll provide a detailed overview of the most useful new features and improvements included in ASP.NET MVC 4 and ASP.NET 4.5, with examples of real-life usage.
Gaston Hillar is a long-time Windows programmer and a frequent contributor to Dr. Dobb's.