Sunday, December 18, 2011

Editing IE settings in the registry via C#

We have a weird setup at work where our Internet Explorer LAN configuration settings are periodically reset by people who manage the network. This brings IE and TFS to a crawl for the programmers. It doesn't seem to happen to everyone all at once or at a designated time. It can take a few minutes to realise what is going on as there are a number of issues that show similar behaviour. For new programmers, this is especially frustrating because they're not aware of this strange policy.

To the Windows registry Batman! I haven't played with this thing in years. I never really understood it very well. "Hierarchical database? All these weird names? Huh? What's going on?!" Seems so simple now.

Editing the registry in the .NET environment is easy. See here.
Finding how to change LAN proxy configuration settings took a little longer to find. Explained here.
Proxy exceptions? See here.

With the app working, all that is left to do is set the script to run every 15mins on the programmers' computers, and we're done.

Code looks like:

var key = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings\Connections"true);
 
if (key == null)
  throw new NullReferenceException("Registry SubKey not found.");
 
const string defaultConnectionSettings = "DefaultConnectionSettings";
var defaultConnectionSettingsValue = (byte[])key.GetValue(defaultConnectionSettings);
 
defaultConnectionSettingsValue[8] = 1;
key.SetValue(defaultConnectionSettings, defaultConnectionSettingsValue, RegistryValueKind.Binary);

Friday, December 16, 2011

Code generation in C#

I wrote my first code generator on the weekend. The real work - generating the C# class files - was trivial. Getting the generator into a state so that it could be easily used was more difficult.

There are a few ways you could do code generation in .NET. I explored:
I went with T4 Templates. They were a little obscure at first but provided the easiest entry point into using the code generator. You just build-up your source file and then right-click and select "Run Custom Tool" on the .tt file to generate the code.

I encounted a few issues when trying to write my template. They were:
  • Extension methods can't be embedded into a T4 Template. There is a workaround. I ended up converting my extension methods into normal methods (I only had two).
  • Accessing your assemblies is difficult in Visual Studio 2010. It has been explained very well. I managed to access my assemblies by writing something like <#@ assembly name="$(SolutionDir)..\DomainEntities\bin\Debug\Cpa.DomainEntities.dll" #>
The example T4 Templates I found on the Internet were a little more complex than what I wanted. (Examples should be as simple as possible.) I wanted to load a source file into memory and generate class files from that. Download my example template if you want something similar. It loads in a CSV file that has two columns (first column is the name of the class, second column the name of the property) and generates some puesdo-code of what the class files will eventually look like. I used this as my base template, then expanded it to create proper C# class files.




Tuesday, November 22, 2011

Comments on Skyrim


I've been playing Skyrim in my (ever dwindling) spare time.

Good things:
  • I play by simply allowing myself to be drawn to whatever seems interesting. I don't particularly care about completing quests, I wander. It's working really well for me. I'm often stumbling onto something interesting.
  • I went on a quest to recover a helmet. (I deliberately chose the most banal of the quests.) It was fun. I crossed over a bridge in a treacherous cavern. I fought a couple of frost trolls. Burnt 'em.
  • I started back to Solitude (a city of Skyrim) and saw the ghost of a headless horseman ride by. It just appeared and rode off. I tried to follow it for a while but was distracted by some midnight revellers who offered me a drink. I drank. When I looked back, the horseman was gone.
  • I saw some rabbits hopping around underwater. Then I saw a white wolf walking underwater. Mammals in Skyrim have strong lungs.
  • On the quest to "find King Olaf's verse," I made my way to the body of Svaknir, at the bottom of a stairway. I was expecting to find a book here. There was no book. My character became trapped in a dungeon, never to escape. (Some bugs are cool.)
Bad things:
  • Books in Skyrim are boring. Tip for next time: Bethesda, if you're going to have poorly written text, write less of it and link it to the gameworld in some way. I only open books because of the possible quests or skill bonuses.
  • Food appears to be useless. They've put so much effort into it, it would have been good to have a requirement where you need to eat and drink occasionally to keep your stamina up.
  • The UI is bad. You can use it and thankfully the core of it (choosing spells and items) is okay, but it's still bad. You have to hit 'tab' to close the menu? What's wrong with 'esc'? See: interface comments.
Skyrim screenshots

Wednesday, November 9, 2011

Occupy Melbourne

I went to my first Occupy Melbourne general assembly this evening (the 16th they've had). It wasn't deliberate, I was walking on Swanston St, eating my chips & tomato sauce, and there they were. I stayed for a couple of hours. My impressions follow.

I'm not very good with numbers, maybe there were 100 people. They used the (predominate) megaphone and the human microphone. Decisions were run on a consensus basis.

It was very much a left ghetto event. However, it was as though the factions had set their differences aside for participation in Occupy Melbourne.

The first hour was reports from various workgroups (legal, direct action, media, etc.) on what they’d been doing since the last GA. The legal team were going to court over whether Occupy Melbourne could have structures and political posters at Treasury Gardens. They wanted people to await the outcome of legal proceedings before Occupy Melbourne decided to do anything more.

I was disinterested by the first hour. If I were a part of Occupy Melbourne, maybe I’d have been more interested (but probably not).

After the reports there were some more substantial suggestions. The first motion was that Occupy Melbourne has a minute silence for the dead of WWI. I was shocked. I had thought that nothing so conservative would ever dare to be tabled. A show of hands for, then against. There was massive dissent. It was quickly reformulated as "all that have died in all wars". There was massive dissent. Others spoke against war per se. They didn’t want to honour victims, they wanted to attack the perpetrators of war, the 1%. I was encouraged by this, but by then, 10-20min of my life had already disappeared before the motion was thrown out. (I definitely would have walked away forever if it had been accepted in any of its forms.)

After, there was a proposal that we build structures on Saturday, regardless of the legal outcome. There was general (60-70%) support for this. One dissenter made an absurd analogy to guerrilla tactics saying "now is not the time to attack but harry the enemy." This received quite a bit of support. One commenter riposte with "This isn't the Vietnam war." Another dissenter talked of not wanting to jeopardise legal proceedings (the interim outcome, everyone already knew, would be known before Saturday). This rhetorical nonsense (clearly suffering for causal misdirection), received even more support than the guerrilla fighter. However, speakers for or against were unable to sway the numbers significantly. (I voted for occupation with structures, too horrified by the nonsense of the dissenters to abstain any longer.) It was decided to revisit the issue on Saturday. After that, I left.

I was generally quite impressed. For all my critical thoughts, it seemed like a movement with promise. Are the "Occupys" around the world the beginnings of a 21st century soviet? I don't know. I don't think anyone could know that. They probably aren't though. They'll probably fizzle and burn out. To avoid that fait, they need to inspire people to take control of their lives. I have no idea how they're going to do that. Hundreds of years of failure gnaws at the edges of the general assembly.

Friday, October 7, 2011

Steve Jobs once said

"Live every day as though it were your last." Absolutely, do not do this. You will be wrong for every day except for one.

Wednesday, September 14, 2011

Some funny code...

I often sigh while reading the source code at me new job. Occasionally, I have a few laughs too. For example:

            catch (Exception exception)
            {
                throw new Exception(exception.ToString());
            }


I'm not sure what it is about exception handling but it seems to be the most frequently mis-understood/mis-used area of programming C#. The lines above are not nearly has bad as:

            catch (Exception exception)
            {
            }

which I've seen a lot of too, but it's more of a sigh moment than laugh.

Thursday, August 25, 2011

I ran home after work tonight...

I ran home after work tonight; first time ever. What would spur on such extreme activity? I've been wanting to run/ride more frequently, but it's been difficult to find the motivation. I thought back to a time where I always tried to ride just that little bit harder. Turns out that was when I had a bike computer. It was decided, geekdom is to be my exercise motivator.

Last week I bought the Forerunner 310XT, designed for running and riding. It has a heart rate monitor, cadence monitor, and a GPS for tracking a whole bunch of data.

After installing a bunch of drivers and plugins, I updated the 301XT's firmware and was ready to go... Except first you need to plug in your height, weight and age for energy burning calculations. Then setup the display screens, make sure the HRM is detected, turn on/off auto settings like auto pause (for traffic lights, etc.), auto lap, and auto scroll. It's all very detailed. Thus far, however, I've been very happy with the features and user interface.

I think the exercise plan is going to work. I barely noticed that I was running, too busy looking at BPMs, time/distance, and trying to outrun my virtual partner that was doing 5:45 minute kms. The jerk beat me by 2mins.

My first run is recorded on the Garmin website.