MetaTrader Indicators
2009-05-05

One of my hobbies is forex trading, and a sub-hobby of that is writing custom indicators. MetaTrader is a trading platform that is very popular in the forex community because it allows traders to write custom indicators. It’s pretty easy to get started if you have programming experience because the language is very similar to C. And yes, you can also write your own bot if you wanted, called an Expert Advisor. Read on...
A Trick Question With Closures
2009-04-28

Given the following code, what do you expect the output to be? for (int i = 0; i < 100; ++i) { ThreadPool.QueueUserWorkItem(delegate { Console.WriteLine(i); }); } Keep that answer in your head! Now…what do you expect the output of the following? int i; for (i = 0; i < 100; ++i) { ThreadPool.QueueUserWorkItem(delegate { Console.WriteLine(i); }); } Were your answers the same? Different? Why?
A Simple Thread Pool Implementation
2009-04-27

I’ve always wanted to do this, and finally I’ve gotten around to doing it. Here’s a super duper simple implementation of a working thread pool. I named in ThreadQueue just so it’s clearly discernible from the one provided by the framework. public static class ThreadQueue { static Queue<WorkItem> _queue = new Queue<WorkItem>(); struct WorkItem { public WaitCallback Worker; public object State; } static ThreadQueue() { for (int i = 0; i < 25; ++i) { Thread t = new Thread(ThreadWorker); t.IsBackground = true; t.Start(); } } static void ThreadWorker() { while (true) { WorkItem wi; lock (_queue) { while (_queue.Count == 0) { Monitor.Wait(_queue); } wi = _queue.Dequeue(); } wi.Worker(wi.State); } } public static void QueueUserWorkItem(WaitCallback callBack, object state) { WorkItem wi = new WorkItem(); wi.Worker = callBack; wi.State = state; lock (_queue) { _queue.Enqueue(wi); Monitor.Pulse(_queue); } } } As you can see, it’s very short and very simple. Read on...
A Simple Thread Pool Implementation (Part 2)
2009-04-27

I said last time I’d compare the performance of my super duper simple implementation against the .NET ThreadPool, so here it is! Here’s the test code: ManualResetEvent evt = new ManualResetEvent(false); int total = 100000; int count = 0; DateTime start = DateTime.Now; for (int i = 0; i < total; ++i) { // ThreadQueue.QueueUserWorkItem(delegate(object obj) ThreadPool.QueueUserWorkItem(delegate(object obj) { Console.WriteLine(obj); if (Interlocked.Increment(ref count) == total) evt.Set(); }, i); } evt.WaitOne(); Console.WriteLine("Time: {0}ms", (DateTime.Now - start).TotalMilliseconds); Console.ReadLine(); Here are the initial tests. Read on...
SourceGear Vault, Part 4, Conclusion
2009-04-25

I suppose I should give a disclaimer since I am not an expert with Vault, and my opinions may be completely due to my lack of understanding of the system. With that in mind, here’s what my experience of using Vault has been so far. In general, it is not as fast as TortoiseSVN. There are many operations in SVN that are instant, where the comparable operation in Vault is met with 10 seconds of “beginning transaction” and “ending transaction”, sometimes more. Read on...