Over the years as a developer, I’ve come to realise that I have encountered similar problems and have had to write same kind of code. I’m sure most of us would have gone through that kind of experience, when we wished we had saved the code snippet or functions in a central area so we can re-use it.

I’ve decided to do exactly that. I’ve created a personal library where I want to store useful, reusable code that helps me in my everyday work, and hopefully will be useful to others too. Coming up with a name was hard, so I’ve decide to concatenate my name and the work “Lib” together, which becomes EdLib.

Read the rest of this entry »

Posted in EdLib. Tags: . 2 Comments »

Linqify your code.

I’m sure most programmers are familiar with Linq, and agree with me that it’s great. I personally really enjoying using Linq, mostly in the form o f Linq to SQL and Linq to Objects. It also helps with simplifying your code and makes it easier to read. Some of the most useful Linq functions I find are FirstOrDefault, Any, All and Where. However sometimes I still come across developers writing loops to do work when Linq can be utilized to simplify it. So here are some simple code samples on how to use Linq to eliminate writing foreach loops.

Read the rest of this entry »

ASP.NET MVC 3: Agnostic Inversion of Control

ASP.NET MVC has matured over the years and I’ve had the fortune to be able to do some development work recently on this. One of the first things I explored was to setup an Inversion of Control (IoC) container. One of the biggest benefits of using an IoC is to accommodate for the ease of unit testing using a mocking framework.

Anyways we use Castle Windsor on our project, but in theory we should be able to use any IoC framework.  I started to think about how to decouple the project from a specific IoC framework, so if we ever wanted to change to a different one, it would be easy and straightforward. Perhaps this is overkill in the real world, but would be a good exercise to go through.

Read the rest of this entry »

Enable Software Rendering in WPF programmatically

From my previous post, I talked about troubleshooting WPF graphic issues. One of those options is to set a registry key to enable software rendering for WPF on that machine only. But that can be an intrusive setting and may not suit everyone’s needs.

There is a another option, which is to enable software rendering in code. If you are working on .NET 3.5 framework, there’s a way to set that up..unfortunately you have to set that up per window or per target control. But might be useful for those that want to selectively tweak rendering mode for each window.

private void ConfigureSoftwareRendering()
    if (shouldEnableSoftwareRendering)
        HwndSource hwndSource = PresentationSource.FromVisual(this) as HwndSource;
        HwndTarget hwndTarget = hwndSource.CompositionTarget;
        hwndTarget.RenderMode = RenderMode.SoftwarewareOnly;

If you’re developing in .NET 4 framework, then you have another option, which is to set up software rendering per process….like so.

public partial class App : Application
    protected override void OnStartup(StartupEventArgs e)
         if (shouldEnableSoftwareRendering)
            RenderOptions.ProcessRenderMode = RenderMode.SoftwareOnly;

You might be wondering why we should use software rendering for WPF applications. It depends on the end-user hardware configuration which might have “WPF-unfriendly” drivers, or just no graphics card. In some situations software rendering might be faster than hardware rendering … hope this helps.

Share this post :
Posted in WPF. 4 Comments »

Graphic issues with WPF

Graphic issues with WPF are not uncommon at all. This may be partially due to graphic card drivers having varying effects with hardware rendering on WPF applications . The same application may look different on PCs with different hardware specifications. In our situation, we had an image rendering with jagged lines on a couple of PCs, and looked fine on others.

In order to troubleshoot if it’s a hardware problem, or your code, I find that the simplest approach is to first disable hardware rendering on your machine, by modifying a registry key. That registry key might not be on your machine, so add it if it isn’t.  By default if that key is absent, WPF assumes hardware rendering should be enabled.

After you reboot, and the graphic problem has disappeared because your PC is now using software rendering, you know for sure that your code is fine. Next you will have to update your graphics card driver, computer bios, and make sure you have at least .NET Framework 3.5 SP1 installed.

Here’s a link for Guidelines for troubleshooting graphic issues on WPF. It’s quite useful, and covers what I discussed above. Give this a go, it has helped identified our graphics problem very swiftly.

Posted in WPF. 1 Comment »

Declarative state of mind

Over the years, we have seen C# language evolve into a more dynamic language, and also have seen numerous frameworks and APIs out there implementing a more declarative style. Famous example would be LINQ, and one can appreciate how the language flows and how readable it is. For too many years we have programmed in a procedural manner, and we have forgotten who the target audience for our code is…..yes the audience is us, the programmers!!!!

Procedural code can be hard to read, which makes code hard to maintain. To be declarative is to write code in a way that declares your intention fluidly using fluent interfaces. I’ve come up with an example that illustrates this.

Read the rest of this entry »

Posted in C#. Tags: . Leave a Comment »

Parallel MSBuild

Did you know MSBuild can now run your build using all the cores on your proccessor? So if your development machine has multi-cores, and your solution is fairly large (perhaps more than 15 projects, but it varies), and you’re developing in .NET Framework 3.5 or higher, you should give parallel builds a go, and see if there’s any savings in compile time.

Scott Hanselman has 2 great articles on this. He explains how to incorporate parallel builds into Visual Studio, and also talks about how to run parallel build in your build process and scripts.

Take note that it’s not a guarantee that it is always going to make your solution build faster, it also depends on your machine specifications as well as how your project dependency structure. If that structures is linear rather than a tree structure, there would not be much benefit running parallel builds. Also take care about how your project references are used, because running parallel build might cause some unexpected locking problems.

On a more positive note, I’m working on a solution with around 57 projects, and using an 8 core PC. Running parallel build, it shaved off 25 seconds off the usual 2 minutes build. For argument’s sake, if I build my solution say 50 times a time (which is a very optimistic value in my opinion),  I save about  20 minutes a day from compiling (assuming you have an 8 hour work day). That’s a big savings, and that benefit grows over the weeks and months for every developer on your team. Don’t forget your build scripts and automated builds too!