[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Passing arguments to a Thread in VB.NET 2010

Posted on 2011-02-16
12
Medium Priority
?
1,367 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 3
12 Comments
 
LVL 86

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
Free Backup Tool for VMware and Hyper-V

Restore full virtual machine or individual guest files from 19 common file systems directly from the backup file. Schedule VM backups with PowerShell scripts. Set desired time, lean back and let the script to notify you via email upon completion.  

 
LVL 12

Accepted Solution

by:
jmcmunn earned 2000 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 2000 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
 

Author Closing Comment

by:highrate
ID: 34929159
Thanks!
0
 
LVL 86

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 86

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 86

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
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…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
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…

649 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