How to apply threading in vb.net with passing an object of a custom class for a method
<p>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.</p>

<p>I have created a custom class as below:</p>

<pre><code>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
</code></pre>

<p>Now in a different module I am creating the instance of this class as below:</p>

<pre><code>Dim X1 As New TestClass("Some String",&lt;Some Object reference&gt;)
Dim X2 As New TestClass("Some String","Some String")
</code></pre>

<p>Now I have declared a sub in the same module like below</p>

<pre><code>Sub DoMyStuff(ByRef A1 As TestClass)
    A1.TestSub()
End Sub
</code></pre>

<p>After all This I want to create a thread and run the sub "DoMyStuff" by Passing a reference of X1</p>

<p>To Do this I have Imported System Threading:</p>

<pre><code>Imports System.Threading
</code></pre>

<p>Inside Module after initialization of X1 and X2 I have written:</p>

<pre><code>Dim T1 as New Threading.Thread(AddressOf DoMyStuff)
T1.Start(X1)
</code></pre>

<p>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()'.</p>

<p>If I am writing like this, </p>

<pre><code>Dim T1 As Threading.Thread
T1 = (AddressOf DoMyStuff)
T1.Start(X1)
</code></pre>

<p>I am getting the error: 'AddressOf' expression can not be converted to 'System.Threading.Thread' because 'System.Threading.Thread' is not a delegate type.</p>

<p>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.</p>

<p>Thanks</p>

<p>Edit:
I have checked your reference Michael Z. and its really helpful. First of all a big Thank you for that :) </p>

<p>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:</p>

<pre><code>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
</code></pre>

<p>Now I have Declared in my module as follows:</p>

<pre><code>Dim T as Threading.Thread = New Threading.Thread(AddressOf DoStuff)
</code></pre>

<p>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:</p>

<pre><code>Public Sub DoStuff() 
   If TextBox1.InvokeRequired Then
      TextBox1.Invoke(T1,"Hello")
   End If
End Sub
</code></pre>

<p>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 :(</p>

<p>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.</p>

<p>Thanks</p>