C#

About

Tuesday, 30 June 2015

async and await are pair keywords. You cannot use them in a standalone manner. async is marked on a method. This keyword is just an indicator saying that this method will have the await keyword. The await keyword marks the position from where the task should resume. So you will always find this keyword in conjunction with Task.

2: Zip facility (Zip compression)

System.IO.Compression.FileSystem System.IO.Compression

using System.IO.Compression; If you want to Zip files from a folder you can use the CreateFromDirectory function as shown below. ZipFile.CreateFromDirectory(@"D:\data",@"D:\data.zip");

If you wish to unzip, you can use the ExtractToDirectory function as shown in the below code. ZipFile.ExtractToDirectory(@"D:\data.zip", @"D:\data\unzip");

3: Regex timeout (TimeOut) try { var regEx = new Regex(@”^(\d+)+$”, RegexOptions.Singleline, TimeSpan.FromSeconds(2)); var match = regEx.Match(“123453109839109283090492309480329489812093809x”); } catch (RegexMatchTimeoutException ex) { Console.WriteLine(“Regex Timeout”); }4: Profile optimization (Improved startup performance) An order to bring down this startup time, in .NET 4.5, we have something called “profile optimization”. Profile is nothing but a simple file which has a list of methods which the application will need during startup. So when the application starts, a background JIT runs and starts translating IL code for those methods into machine / native code.

This background JIT compilation of startup methods happens across multiple processors thus minimizing the start up time further. Also note you need to have a multicore box to implement profile optimization. In case you do not have a multicore box then this setting is ignored.

5: Garbage collector (GC background cleanup) when the GC runs for cleanup, all the application threads are suspended. You can see in the above figure we have three application threads running. We have two GCs running on separate threads. One GC thread for one logical processor. Now the application threads run and do their work. Now as these application threads are performing their task they also create managed objects.

At some point of time the background GC runs and starts clean up. When these background GCs start cleanup, they suspend all the application threads. This makes the server/application less responsive for that moment.

To overcome the above problem, server GC was introduced. In server GC there is one more thread created which runs in the background. This thread works in the background and keeps cleaning generation 2 objects thus minimizing the load on the main GC thread. Due to double GC threads running, the main application threads are less suspended, thus increasing application throughput. To enable server GC, we need to use the gcServer XML tag and enable it to true.