[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1368
  • Last Modified:

Passing arguments to a Thread in VB.NET 2010

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
highrate
Asked:
highrate
  • 5
  • 4
  • 3
2 Solutions
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
jmcmunnCommented:

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
 
highrateAuthor Commented:
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
Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

 
jmcmunnCommented:

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
 
highrateAuthor Commented:
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
 
jmcmunnCommented:

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
 
highrateAuthor Commented:
Thanks!
0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
?

...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
 
jmcmunnCommented:

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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
 
jmcmunnCommented:

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
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Hahahaha....noooo....that's backwards.  =)

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

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now