Example 4
Multithreading with the ThreadPool
1: Private Delegate Sub AddInvoker(ByVal Elem As String) 2: Private Sub AddElem(ByVal Elem As String) 3: ListBox1.Items.Add(Elem) 4: End Sub 5: 6: Private Sub ThreadedLoadList(ByVal state As Object) 7: Dim I As Integer 8: 9: Try 10: For I = 0 To 100000 11: If (Me.InvokeRequired) Then 12: Me.Invoke(New AddInvoker(AddressOf AddElem), _ 13: New Object() {I.ToString()}) 14: End If 15: Next 16: Catch 17: Thread.CurrentThread.Join() 18: End Try 19: 20: End Sub 21: 22: Private Sub Form1_Load(ByVal sender As System.Object, _ 23: ByVal e As System.EventArgs) Handles MyBase.Load 24: 25: AddHandler Application.Idle, AddressOf OnIdle 26: 27: ' 2: Demonstrates multithreaded approach using the thread pool 28: ThreadPool.QueueUserWorkItem(New WaitCallback( _ 29: AddressOf ThreadedLoadList)) 30: 31: Splash.Splash() 32: 33: End Sub