Moving over to

Having become a GNU toolchain developer, I have decided to stop blogging here and continue blogging at Mostly because I don’t think it’s appropriate to blog about non-Microsoft related work under the domain. If you have subscribed to this blog, please reset your rss feed URL to instead.

WinMacro 2.0 beta released

WinMacro is a tiny little application that can record and replay keyboard and mouse actions that you do on your Windows desktop. It’s similar to the macro facility in Word and Excel, but works across applications. I wrote the initial version nearly 5 years back, and it proved to be very popular, with approximately 30000 downloads and tons of email from users. Most of the emails were appreciative, and some of them touching, especially those that described how WinMacro was helping people do a better job in fields ranging from cancer research to network testing. There were quite a few … Continue reading WinMacro 2.0 beta released

Mutating value types – Part 2

In the previous blog post, we found that mutating a struct inside a class works if the struct is declared as a field, but doesn’t work if it is declared as a property. The reason is fairly obvious – if struct fields also returned a copy, then there wouldn’t be any way of mutating the instance at all, even from within the declaring class. 1: struct S 2: { 3: public int X; 4: } 5: 6: class C 7: { 8: public S S; 9: 10: void SetX() 11: { 12: this.S.X = 1; // Won’t work if this.S … Continue reading Mutating value types – Part 2

Finding undisposed objects

If you aren’t familiar with implementing the Dispose/Finalize pattern to clean up resources, stop and read this first. It is really important to get the implementation of the pattern right, or you’ll run into a performance problem at best, or risk leaking and running out of resources at worst. Assuming that you’ve implemented the pattern correctly for your types, how do you guarantee that Dispose is called on all finalizable types in your application? Sure, you have finalizers, but depending on them for cleanup is bad because 1. There is a definite cost to finalization. An object for which a … Continue reading Finding undisposed objects

With great power comes great responsibility

C++ itself is a pretty complex language, and C++/CLI, with its own baggage of things like handles and managed references doesn’t make it easy to read or debug code at a glance. Here’s a piece of code that had me head-scratching for a while. enum class Options { Yes, No, Maybe}; void Func(Options ^g) { Console::WriteLine(g == Options ::Yes); } void Func() { Func(Options::Yes); } If you run the above piece of code, it will print False. Pretty weird huh? It seems inexplicable, until you notice the caret sign (^) before g in the formal parameter declaration of Func. The … Continue reading With great power comes great responsibility

Itertools for C# – Cycle and Zip

Continuing our implementation of Python iterator functions in C#, we’ll implement Cycle and Zip this time. Cycle IEnumerable Cycle(IEnumerable iterable) Cycle keeps cycling through the enumerator, that is, it cycles back to the first element when the enumerator is exhausted. You can use it like IEnumerable<int> source = new List<int>() { 1, 2, 3}; foreach(int element in Itertools.Cycle(source)) { Console.WriteLine(element); // prints 1 2 3 1 2 3.. } The implementation is again very straightforward – all we have to do is wrap the foreach over the iterable in an infinite loop. The outer loop while loop creates a new … Continue reading Itertools for C# – Cycle and Zip

Itertools for C#

If you are a C# developer and haven’t taken a look at the itertools python module so far, you owe it to yourself to learn about it. It’s a module with functions that operate on what Python calls “iterables” (IEnumerable in C#). Go ahead, take a look at it before reading on. This got me interested about implementing similar functions in C#, using IEnumerable<T>. I fired up VS 2008 and started cranking out the code, only to find that most of the itertools functions are already implemented as extension methods on IEnumerable<T> in .NET 3.5. Some of the functions even … Continue reading Itertools for C#

Locator – my first Windows Mobile App

A month back, I bought a new mobile phone (HTC S710) running Windows Mobile 6.0. I specifically bought it for playing around with the Windows Mobile API, and I’ve been trying out a few things with it. I’m pretty impressed by the .NET Compact Framework, it makes it ridiculously simple to do things that require huge chunks of code in C++. Anyways, one of the few things I wished I could do with my old phone was to use the location information broadcast by cellular networks. Almost all GSM providers (Airtel/BSNL/Vodafone) broadcast the current location to the cellphone – the … Continue reading Locator – my first Windows Mobile App


I don’t think anybody except the guys who coded the GC knows the list of triggers for garbage collection. I believe it’s not part of the CLR spec and I guess that was a deliberate decision to allow implementors flexibility in their GC algorithms. That said, there are some situations which leave one confounded as to why the heck the GC doesn’t run. Take a look at the following program.#include “stdafx.h” #include “windows.h” using namespace System; ref class Allocator { LPVOID ptr; public: Allocator() { ptr = VirtualAlloc(NULL, 1024 * 1024 * 1024, MEM_RESERVE, PAGE_EXECUTE_READWRITE); if (ptr == NULL) Console::WriteLine(“Virtual Alloc failed”); … Continue reading GC::PleaseRunWhenProcessRunningOutOfMemory()

How could this work?

I’m sure most of you know about BackgroundWorker, a new component in .NET 2.0, that allows UI code to run tasks asynchronously, without running into cross thread update issues. But what would happen if the thread that launches the background worker is not a UI thread i.e. it doesn’t run a message pump? The answer is quite surprising and caused a piece of working code like this AutoResetEvent waitForWorker = new AutoResetEvent(false);void Method(){ BackgroundWorker worker = new BackgroundWorker(); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_Completed) worker.RunAsync(); waitForWorker.WaitOne();}void worker_Completed(…){ waitForWorker.Set();} to stop working after making a tiny modification like this class MyForm : Form … Continue reading How could this work?