Solved

Passing arguments to a Thread in VB.NET 2010

Posted on 2011-02-16
12
1,353 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
Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

 
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
 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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 document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

707 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