Wednesday, 27 March 2013

Run 32bit .Net application with >2GB RAM on 64bit machine

To allow your 32bit application use more than 2GB of RAM you need to modify the *.exe file with editbin utility. (open ‘Visual Studio Command Prompt’ and it will be on the path)

editbin /LARGEADDRESSAWARE <your-app.exe>

or just add these 2 lines to your Post-build event in Visual Studio

call "$(DevEnvDir)..\tools\vsvars32.bat"
editbin /LARGEADDRESSAWARE "$(TargetPath)"

or these if you are building your project with msbuild outside of Visual Studio

call "%VS100COMNTOOLS%\vsvars32.bat"
editbin /LARGEADDRESSAWARE "$(TargetPath)"

To check if all ok run dumpbin utility as below and check if the output has ‘Application can handle large (>2GB) addresses’ text in FILE HEADER VALUES.

dumpbin /headers <your-app.exe>

Tuesday, 5 March 2013

C# Extension to split IEnumerable into batches of n items

This code splits a list into a batches on n items. If list = (1,2,3,4,5,6,7,8,9,10) and n = 3 then the result is ((1,2,3),(4,5,6),(7,8,9),(10))
public static class Extensions
{
  public static IEnumerable<IEnumerable<T>> Batch<T>(this IEnumerable<T> list, int batchSize)
  {
    int i = 0;
    return list.GroupBy(x => (i++ / batchSize)).ToList();
  }
}
[TestFixture]
public class TestExtensions
{
  [Test]
  public void TestBatch()
  {
    var list = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
    var result = list.Batch(3).ToList();
    Assert.AreEqual(4, result.Count());
    Assert.AreEqual(3, result[0].Count());
    Assert.AreEqual(3, result[1].Count());
    Assert.AreEqual(3, result[2].Count());
    Assert.AreEqual(1, result[3].Count());
    result.ForEach(x=>Console.WriteLine(string.Join(",",x)));
  }
}