Solved

Passing arguments to a Thread in VB.NET 2010

Posted on 2011-02-16
12
1,339 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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility

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
Comment Utility
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
Comment Utility

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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Closing Comment

by:highrate
Comment Utility
Thanks!
0
 
LVL 85

Expert Comment

by:Mike Tomlinson
Comment Utility
?

...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
Comment Utility

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
Comment Utility
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
Comment Utility

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
Comment Utility
Hahahaha....noooo....that's backwards.  =)

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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