ThreadPool.QueueUserWorkItem - Order not preserved?
.net, c#, multithreading, threadpool
Solution
If you want the tasks to run in serial but on a different thread than the calling thread, then you should look into the EventLoopScheduler in the Reactive Extensions. It allows you to schedule units of work on a particular worker thread.
Problem
I just noticed that the order of callbacks queued via `ThreadPool.QueueUserWorkItem` is not deterministic, it certainly isn't the order the callbacks have been passed in. This can be verified with the following simple program: ``` private static void Main() { for (var i = 0; i < 10; ++i) ThreadPool.QueueUserWorkItem(Console.Write, i + " "); Thread.Sleep(1000); } ``` The output of one run is: ``` 0 3 8 9 1 2 5 4 6 7 ``` The name suggests that the order is preserved. Is there a way to make sure that the order is preserved? If not, what alternative implementation do you suggest?