Hello,

I am a newbie with vb 2010 but some experience with vb6

My problem is relatively simple but i cant find the answer to it
I have a form with a textbox and a button on it
when i press the button i have code that starts a sub (in a new thread) located in another class module
let's say this sub should give a msgbox with the text in the textbox.

I tried a number of ways but i only seem to get de default text.
I mean the prewritten text, if i change it at runtime before presing the button it still gives me the default value


It's a serious probblem for my project so i would really apreciate some help.

Thank you.
Daniel

Recommended Answers

All 9 Replies

Could you please post your code, or at least relevant parts of the code here. Someone will take a look at it and help you. Thanks!

Imports System.Threading
Public Class Form1
    Private Thread1 As Thread

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Clas As New Class1
        Thread1 = New Thread(AddressOf Clas.msg)
        Thread1.Start()
    End Sub
End Class
Public Class Class1
    Public Sub msg()
        MsgBox(Form1.TextBox1.Text)
    End Sub
End Class

I took only the relevant code so it can be easyer to look at.


Thank you

You have to pass the text as a parameter. New Thread(AddressOf <delegate>) accepts only delegates with one parameter of type Object.

Here's the button click event with some modifications

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim Clas As New Class1

    ' Change default text "Default Text" of the textbox
    TextBox1.Text = "New text"

    Dim startDelegate As System.Threading.ParameterizedThreadStart ' New Thread() takes this type as a parameter
    startDelegate = New System.Threading.ParameterizedThreadStart(AddressOf Clas.msg) ' Actual value will be a reference to Clas.msg() method
    Thread1 = New Thread(startDelegate) ' New Thread(AddressOf Clas.msg) does also work
    Thread1.SetApartmentState(ApartmentState.STA) ' Had to use single threaded apartment model
    Thread1.Start(TextBox1.Text) ' Pass an object to the thread 

End Sub

Notice that Textbox1.Text is passed through thread's Start method.

The class has to be modified slightly

Public Class Class1

    Public Sub msg(ByVal value As Object)
        MsgBox(value.ToString)
    End Sub

End Class

Method msg takes one argument of type Object and this is how the text from the form's textbox gets passed.

I also found this article: Threading parameters. Threads and parameter passing is clearly explained in that article.

HTH

Thank's a lot, it works fine but this solution doesent solve my entire problem.
I want to pass data from more then one textbox to the thread. Is there a way to do this? or another way of doing it all toghether?
My project will need a lot of threads and a lot of data passing around.

Thnk you,
Daniel

I want to pass data from more then one textbox to the thread. Is there a way to do this?

Yes, there is. Read "Threading parameters" which I previously suggested. It shows two ways to pass multiple parameters. First is to set class properties before starting the thread. Second is to use ThreadPool class and delegates with multiple parameters.

In your case the former solution might be better. Instantiate classes, pass data with classes properties and then invoke threads.

HTH

commented: It helped more than asked +1

I'm so sorry, i saw the code in your previous post and omited the link.
Thank you again but i might still need your help.
The examples there are with one parameter passed exept for the thread pool exaple witch isnt exactly suitable for what i am using.

Sub Moo()
    Dim instance As New Too
    instance.NewProperty = "hello"
    Dim th As New Threading.Thread(AddressOf instance.Foo)
    th.Start()   
End Sub

Class Too

    Private newPropertyValue As String
    Public Property NewProperty() As String
        Get
            Return newPropertyValue
        End Get
        Set(ByVal value As String)
            newPropertyValue = value
        End Set
    End Property

    Public Sub Foo()
        MsgBox(Me.newPropertyValue)
    End Sub
End Class

can i pass more parameters using this class propery asignment?
I'm sorry if the answer is very simple and i just dont see it but i dont :)
2n6w 5 can pass another parameter using th.Start(parameter) but i might still need more then two parameters passed to the thread class.

Thank you again for helping. I allready understood some new things.

Daniel

Ugh i feel kind of stupid. While writing the code on the real project i figured it out :)
I can create as many properties as i want and pass data.
But i still have one more question. Can i use one property asignment for more values or i need to create one for each value i want to pass.

Thank you

Can i use one property asignment for more values or i need to create one for each value i want to pass.

I suggest using separate properties unless properties are related (i.e. array of temperatures). It makes your code easier to read and maintain.

Of course you could write

' Hold all values in an array
    Private _passAllValues As Object()

    Property passAllValues As Object()
        Get
            Return _passAllValues
        End Get
        Set(ByVal value As Object())
            _passAllValues = value
            ' Take separate values and cast them to right type
        End Set
    End Property

to pass everything in an array of Objects at once, but you'll end up easily with some hard-to-catch error.

If you're using VB.NET 2010, you can save some typing by using auto-implemented properties

Public Class AutoImplementedPropertiesInVBNET2010

    Property myName As String = "John Doe"
    Property autoProperty1 As Integer
    Property autoProperty2 As Integer

    Private Function doSomething() As Integer
        ' Say hello to user and return the sum of two properties
        MsgBox("Hello " & _myName)
        Return (_autoProperty1 + _autoProperty2)

    End Function

End Class

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.