Link to home
Create AccountLog in
Avatar of xfriday
xfriday

asked on

Read and Change Control Value from Thread

Im new to VB.NET and wish to use threading for one of the assignments. I do not know how delegate and invoke works so this actually is a very simple question for someone who knows anything about threading.

I want to read a text box value from a thread and then change this on the Windows Form.

Thats All.
Avatar of Joel Coehoorn
Joel Coehoorn
Flag of United States of America image

Many of the .Net controls are just thin wrappers around the old Win32 (pre-.Net) equivalents.  The old controls were not thread safe, hence the .Net controls that wrap them are also NOT thread safe.  This means you have to invoke them.  All of the controls have a property called InvokeRequired.  If you're in a thread, check that property.  If it returns true, call the control's Invoke method and pass it a handle to the method you want to call.  If you want to change a property, you need to create a function for setting the property that you can pass to Invoke().
It is safe to READ a controls value from a Thread, but NOT to WRITE to it from another Thread.

Here is a simple example demonstating this:  (just a button and a textbox on a form)
Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim T As New Threading.Thread(AddressOf Worker)
        T.Start()
    End Sub
 
    Private Sub Worker()
        Dim curTxt As String
        Dim newTxt As String
        For i As Integer = 1 To 5
            curTxt = TextBox1.Text
            newTxt = curTxt & i.ToString & " "
            SetTextBox(TextBox1, newTxt)
            System.Threading.Thread.Sleep(1000)
        Next
    End Sub
 
    Public Delegate Sub SetTextBoxDelegate(ByVal tb As TextBox, ByVal txt As String)
 
    Private Sub SetTextBox(ByVal tb As TextBox, ByVal txt As String)
        If tb.InvokeRequired Then
            Dim stbd As New SetTextBoxDelegate(AddressOf SetTextBox)
            tb.Invoke(stbd, New Object() {tb, txt})
        Else
            tb.Text = txt
        End If
    End Sub
 
End Class

Open in new window

Avatar of xfriday
xfriday

ASKER

Idle_Mind

Thank You for this code example but however what I need to do is read from a dropdown control and thread will write to a label.

Your code is working perfect when I try this independently but the same code pasted in my solution is not even reading the value from dropdown. Im trying to set a variable as

myVar = me.cbTaskType.Text

where cbTaskType is a dropdown list.

Can you help me on this please.
Avatar of xfriday

ASKER

Im increasing the Point Value to get more exposure and a response from someone who knows how I can read a combobox value from a thread like the example above reads a textbox.
I can't think of any reason why it wouldn't work with your ComboBox...    =\

Show us more of your code and we can help you troubleshoot it.
Avatar of xfriday

ASKER

   Private Sub Worker()
        Dim curTxt As String
        Dim cbText As String
        Dim newTxt As String
        For i As Integer = 1 To 5
            curTxt = TextBox1.Text
            cbText = ComboBox1.Text

             ' ABove is just a ComboBox dropped on form with collection values as Test and Another and when I pressed button, test was in the comboBox1.Text

            newTxt = curTxt & i.ToString & " "
            SetTextBox(TextBox1, newTxt)
            System.Threading.Thread.Sleep(1000)
        Next
    End Sub
Avatar of xfriday

ASKER

Here is an attached file for reference.
EE.pdf
ASKER CERTIFIED SOLUTION
Avatar of Mike Tomlinson
Mike Tomlinson
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account