Passing parameters to a procedure called in thread

How can I pass parameters to a procedure which is called in thread  

'! Spin off a new thread. 
myThread = New Thread(New ThreadStart(AddressOf GetRawFigures)) 
myThread.IsBackground = True 
myThread.Start() 

Procedure GetRawFigures takes an argument as follows: 

Private Sub GetRawFigures(ByVal figures As Integer()) 

End Sub 

When I try to provide it as 

myThread = New Thread(New ThreadStart(AddressOf GetRawFigures(intArray)) 

I get an error saying " 'AddressOf' operand must be the name of a method; no parentheses are needed." 

How can I fix this  Thanks. 


Answer this question

Passing parameters to a procedure called in thread

  • LoganX3

    what the compiler is doing is telling you that you can't pass parameters to a method like that...in other words, when you call a method by starting it on a new thread, you are not allowed to pass it parameters...read my next post for the solution.

    -andy moyer

  • Tim Dawson

    thanks. this wud certainly set in right direction.
  • Rando

    first, you should ask yourself if you really need to use a separate thread. there are not that many cases which warrent it. Will the GetRawFigures method take a considerable amount of time to run  If not, then you probably don't want to use a separate thread...

    what you should do is to create a class that contains your target method. You could call the class RawFigures, and it could contain a method called GetRawFigures, or something like that....then, in the classes constructor you could pass it the variables that you need and set class fields (class level vars) to store the values....

    then, after you store the values, start a new thread and call and point it to the address of "GetRawFigures". Now your problem is solved. That's how you pass parms to a different thread. However, I think you may not really want to  use a d ifferent thread to do what you want it to do, unless there are millions upon millions of elements in you int array.

    oh yea, to get the data back to the main thread, you could raise a custom event that you raise when the "GetRawFigures" proc is done processing. You would want to declare the RawFigures class WithEvents in your calling class and set up an event procedure so that you receive notification when the RawFigures class raises the Finished event.


    Public Class RawFigures
         dim _thread As New Thread(AddressOf GetRawFigures)
         dim _intArray as Integer
         dim _results() as Integer

         Public Sub New(intArray() as Integer)
              _intArray = intArray
              Me._thread.Start()
         End Sub

         Public or Private Sub GetRawFigures
              'crunch your stuff here
              _results = yourResultsHere
         End Sub

         Public Readonly Property CrunchedNumbers As Integer()
              Get
                   Return _results
              End Get
         End Property
    End Class
    </code

    -good luck, andy moyer

  • Passing parameters to a procedure called in thread