VB.Net – Passing Multiple Parameters to a thread call without creating a class object

Ok, so in most cases when we need to make a threaded call we need to pass multiple parameters to the worker function. In VB.net by default it supports one parameter to be passed. But when we need to pass more than one parameter then we have to do a little trick.

Observe how the Object array is passed as a parameter using ByVal. When the array is populatd it will contain the refereces/address of the objects. So passing it by value actually passes the whole set of the references.
Thus this trick works even for ByRef variables/Reference variables/Pointer Variables.

Posted in

4 Comments


  1. Awesome! U shall go to heaven i swear!!

    Reply

  2. Please note that if you have Option Strict On, this method will complain about Late Binding. It’s probably better to store the values in Private variables and then retrieve them later on:

    
    Private m_Value1 As Integer
    
    Private Sub Start()
    m_Value1 = 1
    m_Thread = New Thread(AddressOf Process)    
    m_Thread.Start
    End Sub
    
    Private Sub Process()
    MessageBox.Show(m_Value1)
    End Sub
    

    Reply

  3. Great!!
    I was looking for a trick for ByRef parameters.

    Thanks!!

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *


*