Solved

Passing arguments to a Thread in VB.NET 2010

Posted on 2011-02-16
12
1,341 Views
Last Modified: 2013-11-26
View code, this isn't working.
Private Sub btn_scan_threads_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_scan_threads.Click
        Dim TheThreads(256) As Thread
        Dim i As Integer

        i = 0
        While Not (i = lb_ports.Items.Count)
            ports(i) = lb_ports.Items(i)

            TheThreads(i) = New Thread(AddressOf goconnnect(tb_target.Text,ports(i))
            TheThreads(i).Start()
            i += 1
        End While


    End Sub

    Public Sub goconnect(ByVal ip As String, ByVal port As Integer)

    End Sub

Open in new window

0
Comment
Question by:highrate
  • 5
  • 4
  • 3
12 Comments
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34912024
Create a Class to hold the IP and Port.  Change goconnect() to accept a parameter of ONLY object.  Create an instance of your  class and pass that to Thread.Start().  Cast the object passed to goconnect() back to the class and extract the data.

All together, it might look something like this:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        For i As Integer = 1 To 5
            Dim T As New Thread(AddressOf goconnect)
            T.Start(New MyData("IP #" & i, i))
        Next
    End Sub

    Public Class MyData

        Public IP As String
        Public Port As Integer

        Public Sub New(ByVal IP As String, ByVal Port As Integer)
            Me.IP = IP
            Me.Port = Port
        End Sub

    End Class

    Public Sub goconnect(ByVal data As Object)
        Dim md As MyData = DirectCast(data, MyData)
        System.Threading.Thread.Sleep(3000)
        Debug.Print(md.IP & ": " & md.Port)
    End Sub

Open in new window

0
 
LVL 12

Expert Comment

by:jmcmunn
ID: 34913134

It's also common practice to simply pass in an object array with the values you need...it's really no different and has a bit less overhead with classes etc.

TheThreads(i) = New Thread(AddressOf goconnnect( new object() { tb_target.Text,ports(i) } )

Then just cast each individual item back out in the goconnect method...of course it depends on how many properties you have and how often you will change the code.  A class is a nice easy way to keep track of things, but typically unless you need the class to have methods or have it perform other business logic, a full class is not necessary for something this simple.  Creating a class for everything in your project makes for LOTS of one-time use classes.
0
 

Author Comment

by:highrate
ID: 34928826
The line:

TheThreads(i) = New Thread(AddressOf goconnect(New Object() {tb_target.Text, ports(i)}))

Brings up an error:


Error      1      'AddressOf' operand must be the name of a method (without parentheses).      
Private Sub btn_scan_threads_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_scan_threads.Click
        Dim TheThreads(256) As Thread
        Dim i As Integer

        i = 0
        While Not (i = lb_ports.Items.Count)
            ports(i) = lb_ports.Items(i)
            Dim thetarg As String
            thetarg = tb_target.Text
            TheThreads(i) = New Thread(AddressOf goconnect(New Object() {tb_target.Text, ports(i)}))
            TheThreads(i).Start()
            i += 1
        End While


    End Sub

    Public Sub goconnect(ByVal thobj As Object)

    End Sub

Open in new window

0
 
LVL 12

Accepted Solution

by:
jmcmunn earned 500 total points
ID: 34929021

Yeah sorry...I copied the wrong line of your code I think before?

TheThreads(i) = New Thread(AddressOf goconnect )
TheThreads(i).Start(New Object() {tb_target.Text, ports(i)})

and make sure goConnect takes in an object() obviosuly.
0
 

Author Comment

by:highrate
ID: 34929070
Ok, that works.

I'm too much of a newbie to understand how to extract the two values that were passed
in the object.

    Public Sub goconnect(ByVal theobj As Object)
        MsgBox("blah",theobj.??
    End Sub
0
 
LVL 12

Assisted Solution

by:jmcmunn
jmcmunn earned 500 total points
ID: 34929105

something like:

Public Sub goconnect(ByVal theobj As Object)
var ObjArray = theobj as Object()
string target = ObjArray(0) as string
int port = ObjArray(1) as int
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

Author Closing Comment

by:highrate
ID: 34929159
Thanks!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34929306
?

...so you ended up using the exact approach I posted here:
http://www.experts-exchange.com/Programming/Languages/.NET/Visual_Studio_.NET_2005/Q_26826834.html#34912024

Except MESSIER...
0
 
LVL 12

Expert Comment

by:jmcmunn
ID: 34929454

I would argue readability is not effected in this case, given proper variable names.  And I don't need a wrapper class for every method I have that takes more than one parameter.  I'd have a thousand one-off classes.
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34929530
You're right...given good names, readability hasn't suffered at all.

We've just introduced an array of objects, though, and now must know that the "thing" in the first slot a String, and the "thing" in the second slot is an Integer.  The class makes it a little cleaner and less error prone (though the whole process provided by .Net is lacking since you have to use Object in the first place).  I like the strongly typed approach better and would probably encapsulate the thread in a completely different class that accepts the parameters for the thread within.

I liked the mixed C#/VB syntax in your last post...I find myself doing that all the time.  ;)
0
 
LVL 12

Expert Comment

by:jmcmunn
ID: 34929637

Agreed that a class is more useful beyond a couple of params, especially if you need to manipulate them in any way.  Past two or three, it gets messy quick.

It's hard to write VB...it's just not natural!  Hardly feels like code sometimes!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 34929662
Hahahaha....noooo....that's backwards.  =)

C# is not natural (at least not for me!).  See ya around bud.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Summary Displaying images in RichTextBox is a common requirement with limited solutions available. Pasting through clipboard or embedding into RTF content only support static images.  This article describes how to insert Windows control objects int…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

18 Experts available now in Live!

Get 1:1 Help Now