I have an ArrayList that is set to Friend. Once I click my button “abc” is added to the ArrayList and then the form MsgBoxes out the Count of 1 (Correct).
When I use Threadpool to count the number of objects within the ArrayList it always returns 0.
Example:
Imports System.Threading
Public Class Form1
Friend Alphabet As New ArrayList
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Alphabet.Add("abc")
MessageBox.Show("Main Sub: " & Alphabet.Count().ToString())
ThreadPool.QueueUserWorkItem(New WaitCallback(AddressOf TestIt))
End Sub
End Class
Module MyModule
Public Sub TestIt()
MessageBox.Show("Threaded Sub: " & Form1.Alphabet.Count().ToString())
End Sub
End Module
I am obviously getting some sort of cross-thread issue here but have no idea how to correct this. I usually just setup single threads so this is my first time playing with ThreadPool & already lost at step 1!
When you QueueUseWorkItem you can pass in an object. Then your method will have one paramter only of type object. This will allow you to send the object at the time of the event since the Thread could run at anytime.