Microsoft Reference Source is a Good Friend

With recent versions of .Net, Microsoft has provided reference source to allow you to navigate and debug issues with the .Net framework. The reference source website is a great tool to have in your back pocket when researching how something in the .Net framework works. You can download a full solution (minus some resource files) for use in Visual Studio or you can link directly to a line of code when sending emails or instant messages to colleagues.

Recently I was working with the Managed AddIn Framework, and I was trying to determine how to load settings for an AddIn. I did some reading and really didn’t find much on the subject. I tried creating a .config file for the AddIn and magically it was able to load the settings from there. I don’t like magic in my code, so I wanted to know how the settings were being loaded. I downloaded the reference source and was able to dig into the AddInActivator class and find the calls into the AddInServer. Inside that code, I found that they explicitly load the configuration file based on the AddIn assmebly name:

AppDomain domain;
AppDomainSetup setup = new AppDomainSetup();
setup.ApplicationBase = Path.GetDirectoryName(token._addin.Location);
setup.ConfigurationFile = token._addin.Location + ".config";

I then took this information and was able to build up a nice email to my colleagues with links directly to the lines of code that we needed to understand to know how the MAF framework loaded configuration files.

I encourage every .Net developer to get a copy of the reference source on your machine and leverage it when you need to get a better understanding of how the .Net framework works.

Visual Studio 2013 – Paste JSON as Class

There is a really cool new feature in Visual Studio 2013 that lets you paste JSON or XML and create a class hierarchy from it. For example, if you have the following JSON:

{
"People" :[{
    "First Name" : "John",
    "Last Name" : "Koerner", 
    "Address" : {
        "Street" : "1234 Main",
        "City" : "Somewhere",
        "State" : "MI",
        "Zip" : 12345
    }
  }]
}

You can open up a .cs file in VS 2013, and under the Edit->Paste Special menu, you have the option to Paste JSON as Classes, which will create the following class hierarchy:

public class Rootobject
{
    public Person[] People { get; set; }
}

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public Address Address { get; set; }
}

public class Address
{
    public string Street { get; set; }
    public string City { get; set; }
    public string State { get; set; }
    public int Zip { get; set; }
}

Even though this is a really small feature, it’s just one of those small things that just makes working in visual studio that much easier.

Improving the Performance of the Android Emulator on Windows

This is a collection of tips and tricks I have found that have greatly improved the performance of emulators on windows. When I first loaded the emulator, it took just under 2 minutes to load and get to a point where an app could be started. After making some tweaks, it now takes about 20 seconds.

On the left we have the default ARM setup and on the right we have an x86 emulator setup (at 3x speed):

Android Timings ARM Android Timings x86

The three major factors that contributed to the increase:

  • Using the x86 emulator instead of the ARM emulator
  • Enabling Use Host GPU on the virtual device
  • Ensuring HAX is installed

Using the x86 emulator

To create an x86 emulator, you need to ensure you have downloaded the x86 system image for your android version:

Once that is done, simply create a new emulator using that CPU:

Enabling Use Host GPU

Enabling the host GPU allows your video card to do some of the heavy lifting for display rendering and should give you a good deal of a performance increase. This setting is on the VHD creation dialog:

Enabling HAX

HAX is a hypervisor developed by Intel that was built specifically to improve Android emulation on Intel hardware. You can install this from the Intel website, or run the IntelHaxm executable included with the Android SDK. This can be found at:

extras\intel\Hardware_Accelerated_Execution_Manager

Note that this only works with some intel processors. See the troubleshooting section for more info.

After the install, you can check if the service is working by running sc query intelhaxm. You should see a state of 4 indicating it is running:

$ sc query intelhaxm

SERVICE_NAME: intelhaxm
        TYPE               : 1  KERNEL_DRIVER
        STATE              : 4  RUNNING
                                (STOPPABLE, NOT_PAUSABLE, IGNORES_SHUTDOWN)
        WIN32_EXIT_CODE    : 0  (0x0)
        SERVICE_EXIT_CODE  : 0  (0x0)
        CHECKPOINT         : 0x0
        WAIT_HINT          : 0x0

Also, when you start your emulator, you should see a message indicating HAX is working:

Starting emulator for AVD 'Nexus-7GPU'
emulator: device fd:924
HAX is working and emulator runs in fast virt mode

Troubleshooting

When trying to set this up, I did run into a few snags and there are some things to check if you are getting error messages.

Make sure your processor supports HAX

You can check if your processor supports HAX on the Intel Ark website. Select your processor and under the Advanced Technologies section, look for the value IntelĀ® Virtualization Technology (VT-x) and ensure it is set to yes.

Verify VT-x is enabled in the BIOS

If your processor supports VT-x there may be a setting in the BIOS that enables or disables the feature. Verify that this setting is set to enabled.

Disable any other hypervisors

I found that on my Windows 8 installation Hyper-V was installed and this was interfering with HAX. You have to disable the feature (not just disable the services) in order to work around this. To disable the feature, open Programs and Features, select Turn Windows features on or off and uncheck Hyper-V.

Stop ignoring an update in IntelliJ

Occasionally IntelliJ pops up the update dialog asking me to update to the latest versoin:

Usually I’ll just click Remind Me Later and continue on my way, but this time I clicked the Ignore This Update button. Now anytime I go to check for updates, the event log states:

Update Info
You already have the latest version of IntelliJ IDEA installed.
To configure automatic update settings, see the Updates dialog of your IDE settings

After some digging, I found a post from Dmitry Jemerov on the Jet Brains support forum which stated:

In the current build, you need to edit the settings manually (under in other.xml).

That’s good info, unfortunately it doesn’t provide any information on the location of other.xml. After a quick check of the normal locations, I was able to find other.xml at %userprofile%\.IdeaIC12\config\options.

Once in that file you need to search for myIgnoredBuildNumbers and simply clear out the content, so it looks like this:

<option name="myIgnoredBuildNumbers">
</option>

Now IntelliJ will notify you of the update again and you can keep on telling it to remind you later.

Getting the name of the day of the week in C#

Getting the current day of week in C# is pretty easy if all you need is the English version of the day, for example:

MessageBox.Show(System.DateTime.Now.DayOfWeek.ToString());

But if you want to get the Day of Week for the current language the computer is setup for, the above code will not work. You need to access the DayNames property on the DateTimeFormatInfo class and get the day of week from there. The snippet of code below, demonstrates how to do it:

MessageBox.Show(System.Globalization.CultureInfo.CurrentCulture.DateTimeFormat.DayNames[(int)System.DateTime.Now.DayOfWeek]);