Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Passing variables to new threads

Posted on 2006-05-09
5
Medium Priority
?
203 Views
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.Start()
        ss = Nothing
    End Sub

Here is the sub:

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

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

Expert Comment

by:ZeonFlash
ID: 16641228
I found this article helpful when attempting to do the same thing:  http://www.devx.com/dotnet/Article/11358

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.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 1000 total points
ID: 16641427
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)
    ss.Start()
  End Sub

  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Ping("www.google.com")
  End Sub

Bob
0
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 16641772
Threads can access anything in the object that contains them...be 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"...
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 16641815
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.

Bob
0
 
LVL 1

Author Comment

by:rbichon
ID: 16647598
Thanks. That worked great!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: jpaulino
XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String (http://msdn.microsoft.com/en-us/library/system.string.aspx) Literal, only instead of starting and ending with w…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Suggested Courses

577 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