I decided to change things a little further and did this in the SyncLock block:
vb.net Code:
Console.WriteLine("Work item {0} completed on thread {1} at {2:HH:mm:ss.fffffff}",
value,
Thread.CurrentThread.ManagedThreadId,
Date.Now)
It showed what you might expect, i.e. that each block of completed work items reported using the same threads as the preceding block with addition of one new thread. Clicking the Button a second time in quick succession showed the pattern continuing from where it left of with the same threads. Clicking the Button a third time after a reasonable pause showed the pattern start again at the beginning with a new set of threads. As a result, the second run was significantly faster than the other two (less than half the time) because it had more threads available to process work items. I set the number of work items to a constant 200 and the first and third runs took between 17 and 18 seconds, while the second run took just over 8.6 seconds.
Another option to consider is parallel programming in .NET 4.0. I'm not sure what Framework version you're targeting but here's a reimplementation of my previous code using the Parallel class:
vb.net Code:
Imports System.Threading
Imports System.Threading.Tasks
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim timer = Stopwatch.StartNew()
Dim rng As New Random
'Create a variable number of work items to process.
Dim data As Integer() = Enumerable.Range(0, rng.Next(100, 201)).ToArray()
Parallel.ForEach(data, AddressOf ProcessWorkItem)
'Get the number of work items that must be processed.
Dim workItemCount As Integer = data.Length
'All work items are complete.
MessageBox.Show(data.Length & " work items completed in " & timer.Elapsed.ToString())
End Sub
Private Sub ProcessWorkItem(ByVal data As Integer)
'Process the data.
Thread.Sleep(1000)
Console.WriteLine("Work item {0:000} completed on thread {1:000} at {2:HH:mm:ss.fffffff}",
data,
Thread.CurrentThread.ManagedThreadId,
Date.Now)
End Sub
End Class
In my tests this method was slightly slower than the previous method but the code is certainly easier to follow. The results show basically the same pattern as before except that this time, the work items do indeed seem to be processed in a random order:
Code:
Work item 000 completed on thread 010 at 19:42:21.9162029
Work item 032 completed on thread 006 at 19:42:21.9162029
Work item 096 completed on thread 012 at 19:42:21.9222032
Work item 064 completed on thread 011 at 19:42:21.9222032
Work item 128 completed on thread 013 at 19:42:21.9242033
Work item 001 completed on thread 014 at 19:42:22.9242605
Work item 002 completed on thread 010 at 19:42:22.9392614
Work item 004 completed on thread 013 at 19:42:22.9402615
Work item 065 completed on thread 011 at 19:42:22.9402615
Work item 097 completed on thread 012 at 19:42:22.9402615
Work item 033 completed on thread 006 at 19:42:22.9402615
Work item 006 completed on thread 014 at 19:42:23.9263179
Work item 035 completed on thread 015 at 19:42:23.9263179
Work item 003 completed on thread 010 at 19:42:23.9393186
Work item 005 completed on thread 013 at 19:42:23.9403187
Work item 098 completed on thread 012 at 19:42:23.9403187
Work item 066 completed on thread 011 at 19:42:23.9403187
Work item 034 completed on thread 006 at 19:42:23.9403187
Work item 067 completed on thread 016 at 19:42:24.9253750
Work item 007 completed on thread 014 at 19:42:24.9263751
Work item 036 completed on thread 015 at 19:42:24.9263751
Work item 008 completed on thread 010 at 19:42:24.9393758
Work item 012 completed on thread 013 at 19:42:24.9403759
Work item 068 completed on thread 011 at 19:42:24.9403759
Work item 099 completed on thread 012 at 19:42:24.9403759
Work item 038 completed on thread 006 at 19:42:24.9403759
Work item 072 completed on thread 016 at 19:42:25.9254322
Work item 103 completed on thread 017 at 19:42:25.9254322
Work item 037 completed on thread 015 at 19:42:25.9264323
Work item 016 completed on thread 014 at 19:42:25.9264323
Work item 009 completed on thread 010 at 19:42:25.9394330
Work item 013 completed on thread 013 at 19:42:25.9404331
Work item 100 completed on thread 012 at 19:42:25.9404331
Work item 069 completed on thread 011 at 19:42:25.9404331
Work item 039 completed on thread 006 at 19:42:25.9404331
Work item 104 completed on thread 017 at 19:42:26.9254894
Work item 073 completed on thread 016 at 19:42:26.9254894
Work item 020 completed on thread 018 at 19:42:26.9254894
Work item 017 completed on thread 014 at 19:42:26.9264895
Work item 042 completed on thread 015 at 19:42:26.9264895
Work item 010 completed on thread 010 at 19:42:26.9394902
Work item 014 completed on thread 013 at 19:42:26.9404903
Work item 040 completed on thread 006 at 19:42:26.9404903
Work item 101 completed on thread 012 at 19:42:26.9404903
Work item 070 completed on thread 011 at 19:42:26.9404903
Work item 074 completed on thread 016 at 19:42:27.9255466
Work item 021 completed on thread 019 at 19:42:27.9255466
Work item 105 completed on thread 017 at 19:42:27.9255466
Work item 022 completed on thread 018 at 19:42:27.9255466
Work item 043 completed on thread 015 at 19:42:27.9265467
Work item 018 completed on thread 014 at 19:42:27.9265467
Work item 011 completed on thread 010 at 19:42:27.9395474
Work item 015 completed on thread 013 at 19:42:27.9405475
Work item 041 completed on thread 006 at 19:42:27.9405475
Work item 071 completed on thread 011 at 19:42:27.9405475
Work item 102 completed on thread 012 at 19:42:27.9405475
Work item 106 completed on thread 017 at 19:42:28.9256038
Work item 075 completed on thread 016 at 19:42:28.9256038
Work item 046 completed on thread 020 at 19:42:28.9256038
Work item 023 completed on thread 018 at 19:42:28.9256038
Work item 024 completed on thread 019 at 19:42:28.9256038
Work item 019 completed on thread 014 at 19:42:28.9266039
Work item 044 completed on thread 015 at 19:42:28.9266039
Work item 026 completed on thread 010 at 19:42:28.9396046
Work item 047 completed on thread 013 at 19:42:28.9406047
Work item 078 completed on thread 011 at 19:42:28.9406047
Work item 055 completed on thread 006 at 19:42:28.9406047
Work item 110 completed on thread 012 at 19:42:28.9406047
Work item 107 completed on thread 017 at 19:42:29.9256610
Work item 076 completed on thread 016 at 19:42:29.9256610
Work item 087 completed on thread 018 at 19:42:29.9256610
Work item 086 completed on thread 021 at 19:42:29.9256610
Work item 063 completed on thread 020 at 19:42:29.9256610
Work item 025 completed on thread 019 at 19:42:29.9256610
Work item 091 completed on thread 014 at 19:42:29.9266611
Work item 045 completed on thread 015 at 19:42:29.9266611
Work item 027 completed on thread 010 at 19:42:29.9396618
Work item 048 completed on thread 013 at 19:42:29.9406619
Work item 056 completed on thread 006 at 19:42:29.9406619
Work item 111 completed on thread 012 at 19:42:29.9406619
Work item 079 completed on thread 011 at 19:42:29.9406619
Work item 108 completed on thread 017 at 19:42:30.9257182
Work item 088 completed on thread 018 at 19:42:30.9257182
Work item 124 completed on thread 022 at 19:42:30.9257182
Work item 118 completed on thread 021 at 19:42:30.9257182
Work item 120 completed on thread 020 at 19:42:30.9257182
Work item 077 completed on thread 016 at 19:42:30.9257182
Work item 092 completed on thread 014 at 19:42:30.9267183
Work item 028 completed on thread 010 at 19:42:30.9397190
Work item 049 completed on thread 013 at 19:42:30.9407191
Work item 080 completed on thread 011 at 19:42:30.9407191
Work item 112 completed on thread 012 at 19:42:30.9407191
Work item 057 completed on thread 006 at 19:42:30.9407191
Work item 109 completed on thread 017 at 19:42:31.9257754
Work item 121 completed on thread 020 at 19:42:31.9257754
Work item 119 completed on thread 021 at 19:42:31.9257754
Work item 125 completed on thread 022 at 19:42:31.9257754
Work item 089 completed on thread 018 at 19:42:31.9257754
Work item 093 completed on thread 014 at 19:42:31.9267755
Work item 029 completed on thread 010 at 19:42:31.9397762
Work item 113 completed on thread 012 at 19:42:31.9407763
Work item 050 completed on thread 013 at 19:42:31.9407763
Work item 081 completed on thread 011 at 19:42:31.9407763
Work item 058 completed on thread 006 at 19:42:31.9407763
Work item 126 completed on thread 022 at 19:42:32.9258326
Work item 090 completed on thread 018 at 19:42:32.9258326
Work item 122 completed on thread 020 at 19:42:32.9258326
Work item 094 completed on thread 014 at 19:42:32.9268327
Work item 030 completed on thread 010 at 19:42:32.9398334
Work item 051 completed on thread 013 at 19:42:32.9408335
Work item 082 completed on thread 011 at 19:42:32.9408335
Work item 114 completed on thread 012 at 19:42:32.9408335
Work item 059 completed on thread 006 at 19:42:32.9408335
Work item 127 completed on thread 022 at 19:42:33.9258898
Work item 123 completed on thread 020 at 19:42:33.9258898
Work item 095 completed on thread 014 at 19:42:33.9268899
Work item 031 completed on thread 010 at 19:42:33.9398906
Work item 052 completed on thread 013 at 19:42:33.9408907
Work item 060 completed on thread 006 at 19:42:33.9408907
Work item 083 completed on thread 011 at 19:42:33.9408907
Work item 115 completed on thread 012 at 19:42:33.9408907
Work item 053 completed on thread 013 at 19:42:34.9409479
Work item 084 completed on thread 011 at 19:42:34.9409479
Work item 116 completed on thread 012 at 19:42:34.9409479
Work item 061 completed on thread 006 at 19:42:34.9409479
I've truncated the output a bit because my post was too long but you get the idea.