Passing variables to new threads

Posted on 2006-05-09
Last Modified: 2010-04-23
I want to start a new thread but I am getting an error.

Here is the code:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim ss As Thread = New Thread(AddressOf SendPing)
        ss = Nothing
    End Sub

Here is the sub:

    Sub SendPing(ByVal ip As String, ByVal index As Integer)
    End Sub

How do I start a thread from a subroutine that requires certain variables to be passed to it?
Question by:rbichon
    LVL 17

    Expert Comment

    I found this article helpful when attempting to do the same thing:

    What I ended up doing, though, was to set a global to the value that I needed, and then had the thread snag it upon starting.  Ofcourse, you should look into locking/unlocking the variables before doing that.
    LVL 96

    Accepted Solution

    Here is a thought:

      Private Class ThreadWorker

        Public Sub New(ByVal url As String)
          m_url = url
        End Sub

        Private m_url As String = ""

        Public Sub SendPing()
        End Sub

      End Class

      Private Sub Ping(ByVal url As String)
        Dim worker As New ThreadWorker(url)
        Dim ss As New Thread(AddressOf worker.SendPing)
      End Sub

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      End Sub

    LVL 85

    Expert Comment

    by:Mike Tomlinson
    Threads can access anything in the object that contains that a Form or a Class.  So make the variables global, either literally as ZeonFlash suggests by placing them in a Module, or by placing them at your Form level.

    Or encapsulate your thread in a Class as Bob suggests so that the thread can access those values.  This is usually the preferred method since each thread can have its own set of "parameters"...
    LVL 96

    Expert Comment

    by:Bob Learned
    I have been reading a lot about thread local storage (TLS) and the ThreadStatic attribute, and they don't quite get the job done IMHO.  I love the encapsulation of a property and the thread Start method.

    LVL 1

    Author Comment

    Thanks. That worked great!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
    It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now