multithreading - How to apply threading in vb.net with passing an object of a custom class for a method
I am newbie here. I need your help. I am trying to develop a tool where I want to implement multi-threading in vb.net. I am stuck in a place which I could not resolve. Let me explain you.
I have created a custom class as below:
Public Class TestClass 'Few private Members of my class Private a As String,b As String Public Structure abc Dim xx as object Dim yy as object Dim zz as object End Structure Public aa(10) As abc 'Now I needed to override the constructor as the initialization of 'instance of this class can be made in two different ways due to 'requirement Public Sub New(ByVal xy As String,ByRef yz As Object) 'Some internal method to initialize the object Me.a=xy Me.b=xy End Sub 'Another way to create the instance Public Sub New(ByVal xy As String,ByVal yz As String) 'Some internal method to initialize the object Me.a=xy Me.b=yz End Sub 'Now a public method which I want to call using threading Public Sub TestSub() 'Do something with Me.a,Me.b and aa(some index) End Sub End Class
Now in a different module I am creating the instance of this class as below:
Dim X1 As New TestClass("Some String",<Some Object reference>) Dim X2 As New TestClass("Some String","Some String")
Now I have declared a sub in the same module like below
Sub DoMyStuff(ByRef A1 As TestClass) A1.TestSub() End Sub
After all This I want to create a thread and run the sub "DoMyStuff" by Passing a reference of X1
To Do this I have Imported System Threading:
Inside Module after initialization of X1 and X2 I have written:
Dim T1 as New Threading.Thread(AddressOf DoMyStuff) T1.Start(X1)
Here I am getting error: Overload Resolution Failed because no 'New' can be called with this arguments: 'Public Sub New(Start As System.Threading.ParameterizedThreadStart, maxStackSize As Integer)': Method 'Public Sub DoMyStuff(ByRef A1 As TestClass)' does not have a signature compatible with delegate 'Delegate Sub ParameterizedThreadStart(obj As Object)'. 'Public Sub New(Start As System.Threading.ThreadStart, maxStackSize As Integer)': Method 'Public Sub DoMyStuff(ByRef A1 As TestClass)' does not have a signature compatible with delegate 'Delegate Sub ThreadStart()'.
If I am writing like this,
Dim T1 As Threading.Thread T1 = (AddressOf DoMyStuff) T1.Start(X1)
I am getting the error: 'AddressOf' expression can not be converted to 'System.Threading.Thread' because 'System.Threading.Thread' is not a delegate type.
Might be I am missing some thing, which I am unable to find. I am not so much good in Delegate type concept. Also this is my first project with implementation of Threading. Looked for answers in Google but unable to understand to sort out. Your help highly solicited.
Edit: I have checked your reference Michael Z. and its really helpful. First of all a big Thank you for that :)
However, I am unable to understand a thing in there. If you help me out there, I would be highly grateful. As per my understanding, I have changed my code as below:
Public Delegate Sub Test2(ByRef A1 as TestClass) Public T1 as Test2 = New Test2(AddressOf DoMyStuff) Public Sub DoMyStuff(ByRef A1 as TestClass) A1.TestSub() End Sub
Now I have Declared in my module as follows:
Dim T as Threading.Thread = New Threading.Thread(AddressOf DoStuff)
Here I am unable to understand how to write the DoStuff sub as in your example you were working with TextBox1 which is an object in UI, so you wrote:
Public Sub DoStuff() If TextBox1.InvokeRequired Then TextBox1.Invoke(T1,"Hello") End If End Sub
But here I need to work with an object which is custom made i.e. X1 or X2. Also I need to pass the reference of the object in the Thread so that the method TestSub can work with the created object either X1 or X2. Totally lost here :(
Please correct me if anywhere I am wrong to understand your reference, otherwise if correctly understood, can you Please help me out here. Appreciate your help in advance.